Count the Number of Lines in a Text File

Posted On Thu, 9 Aug 2012

Filed under Batch
Tags: , ,

Comments Dropped leave a response

I have of late, wherefore I know not, started writing little Batch programs. Batch is ugly and buggy, but at the same time its raw minimalism makes it capable of performing surprisingly complicated tasks. Ah, but knowing how to exploit this power, therein lies the rub!

Let's start with the seemingly straightforward task of counting the lines in a text file and storing the result in a variable.

Counting the Number of Lines

Sometimes it's useful to know the number of lines in a text file. You might be tempted to try something like:

find /c /v "" < file.txt

This works because the file is redirected (that's what the < is for) into find so find doesn't know the file's name and can't print it. Type in the command above again, but this time leave out the less than (<) and you'll see what I mean.

The /v "" switch means display all lines that don't contain anything ("" is the empty string). In other words, display all lines. And /c means display the count of the lines that matched.

But this doesn't always give the correct number of lines for files containing trailing newlines or files where the last line isn't terminated by a newline.

The “belt and suspenders” solution is to type the file and pipe the output into find as below:

type file.txt | find /c /v ""

Storing the Result in a Variable

Great, but that just outputs a number. How do you store the number of lines in a variable? The for /f loop to the rescue!

for /f "tokens=1" %%c in ('type file.txt ^| find /c /v ""') ^
do set lines=%%c

The number of lines in file.txt is stored in the lines variable. The pipe (|) character is escaped with a circumflex (^) inside the for /f loop's in (...) clause. The circumflex at the end of the line means that the current line is continued onto the next line. Loop variables like %%c in the example above must be preceded by a single per cent (%) on the command line and two per cents (%%) in a Batch file.

Caveats

The above example only holds true for plain text files with Windows line endings. Results for files with different line endings or containing troublesome control characters may not always be reliable.

Related Links

For more on this topic, read Q64 of Prof. Timo Salmi's famous Batch FAQ as well as this thread on the alt.msdos.batch.nt newsgroup from April, 2006.

If you require a line-counting program with oodles of options, consider the free NUMLINES command line utility from Gammadyne Corporation.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s