Thursday, August 4, 2011

To find the no of lines in the file perl

To find the no of lines in a file a one line command in Perl is sufficient

perl -ne "$. -- if /$^/;END {print $.}" filename

will display the no of lines in the file excluding the blank lines .

Analysis:
"$."  means the current record of the file

"$. --  if /$^/   means the one count will be reduced when it comes across the blank line.

END {print $.} means to print the "$." once it is completed with the file.






No comments:

Post a Comment