how to get the no. of lines in a text file

Hi,

I want to count the no. of lines in a text file and print only the count(without file name) in another file with the prefix 'Count:'.

I'm able to count the no. of lines present in the file using wc -l <filename> but i have to subtract one from the count and print as i dont need to include the header. Please guide to do so.

TIA.

echo -n "Count:" > file2.txt; wc -l file.txt | cut -d' ' -f1 >> file2.txt

Another approach:

awk 'END{print "Count: " NR-1}' file > newfile

necroman08,

The OP needs the number of lines minus one!

Regards

#!/bin/bash
x=-1
while read line;do let ++x;done < file
echo Count: $x >> new_file

O yes, I forgot about that that. So:

 
echo -n "Count: " > file2.txt; echo $( expr `wc -l file.txt | cut -d' ' -f1` - 1) >> file2.txt

Ok, it�s not so elegant as your�s :o

echo "Count: `sed -n '2,$ p' file | wc -l`" > newfile
perl -lne '$.>=2 and $i++; END{print "Count: ",$i}' file > newfile

perl -nE 'push @x,$_; END{say "Count: ",$#x}' file > newfile
 
perl -lne '$i++; END{print "Count: ",$i-1}' file > newfile

perl -lne 'END{print "Count: ",$.-1}' file > newfile

perl -nE 'END{say "Count: ",$.-1}' file > newfile

tyler_durden

Hi All,

Thanks for the response. It was very helpful.

why exactly do you need 'cat'?