Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get number of lines in a text file 1

Status
Not open for further replies.

varakal

IS-IT--Management
Mar 18, 2004
114
US
How can I get total number of lines in a text file using perl from command line. $. gives the line number where the file handle is pointing to, but I need only the last line number.

Thanks.
 
Code:
open FH, "<$filename";
@lines=<FH>;
close FH;

print "$#lines lines in file $filename\n";
HTH


cigless ...
 
Paul, Thanks for your reply, it seems pretty straightforward, but is there a way we can do this from command prompt using perl -e ...

Thanks.
 
Code:
prompt$ >perl script.pl

No??? 8)
--Paul


cigless ...
 
Code:
perl -ne "eof && print $." filename
Easier to just say wc -l filename, though.

 
Or, more awk-like:
Code:
perl -ne "END{print $.}" filename
 
Thanks mikevh, this is exactly what I needed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top