vimalr
August 14, 2009, 9:25am
1
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
methyl
August 14, 2009, 12:21pm
6
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
vimalr
August 17, 2009, 8:31am
8
Hi All,
Thanks for the response. It was very helpful.
why exactly do you need 'cat '?