Monday, August 20, 2012

Counting lines in text files

One of the best ways to enhance error checking in the scripts you write as a DBA is to search for various error messages and codes in log files.  A related question that often comes up during error checking is to count the number of lines in a text file.  The method is different depending on whether you are using Unix or Windows.

On Windows, the best way we've found to count the number of lines in a text file is to run this from the command prompt:

findstr /R /N "^" {filename.txt} | find /C ":"

(Hint:  The ^ character represent the start of a new line, so this is the secret to locating each new line.)

On Unix, we use this method:

wc -l {filename.txt}

or

cat {filename.txt} | wc -l

No comments:

Post a Comment