Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count no of lines in text file and store the value into a variable using batch script?

I want to count the no of lines in a text file and then the value has to be stored into a environment variable. The command to count the no of lines is

 findstr /R /N "^" file.txt | find /C ":" 

I refered the question How to store the result of a command expression in a variable using bat scripts? Then I tried,

 set cmd="findstr /R /N "^" file.txt | find /C ":" " 

I am getting the error message,

 FIND: Parameter format not correct 

How could i get rid of this error.

like image 961
rashok Avatar asked Apr 14 '11 14:04

rashok


People also ask

How do you count lines in DOS?

The /c option could also be used to count all lines that a commands leaves as output. For example we would like to know the number of groups in Active Directory. By using a pipe to FIND /v “” /c we want to see all lines that does not (/v) match the string “”, i.e. all lines and then count it.

What is Find command in Windows?

Searches for a string of text in a file or files, and displays lines of text that contain the specified string.


1 Answers

There is a much simpler way than all of these other methods.

find /v /c "" filename.ext 

Holdover from the legacy MS-DOS days, apparently. More info here: https://devblogs.microsoft.com/oldnewthing/20110825-00/?p=9803

Example use:

adb shell pm list packages | find /v /c "" 

If your android device is connected to your PC and you have the android SDK on your path, this prints out the number of apps installed on your device.

like image 136
user169771 Avatar answered Sep 29 '22 19:09

user169771