Computer Hope

Software => BSD, Linux, and Unix => Topic started by: arz_razi on February 04, 2007, 09:14:04 PM

Title: Row counts
Post by: arz_razi on February 04, 2007, 09:14:04 PM
 I've problem for counting rows of multiple text(.txt) file. How can i use command to get the row count.  
The test files is like this.
      
   txt file1           txt file2
        a,1111          z,999
        b,2222          y,888
        c,3333          x,777
 
I've tried cmd wc -l filename | awk '{print $1}' and the output is  
 
       3  
       3  
       6
 
if cmd like this wc -l filename (without awk), the output,
 
       3 file1
       3 file2
       6 total
 
How can i get only the total value (in above case '6')

Please help me
Title: Re: Row counts
Post by: banjo67xxx on February 06, 2007, 01:09:14 PM
You're nearly there with wc and awk. Just throw tail into the mix and its done.
Code: [Select]
wc -l *.txt | awk '{print $1}' | tail -1
Title: thanks
Post by: arz_razi on February 06, 2007, 09:47:44 PM
thanks..

but how about this one above...

$cat file.txt ... then the output

Name : razi
Acc   : 1111
Name : ana
Acc   : 2222
Name : John
Acc   : 3333

How can i select only the NAME ROW.

The output that i desire is

Name : Razi
Name : Ana
Name : John

Do assist me..thanks



Title: Re: Row counts
Post by: banjo67xxx on February 07, 2007, 02:36:00 AM
There are 2 ways to solve this.

Throw in another command
Code: [Select]
grep '^Name :' *.txt | wc -l| awk '{print $1}'
Or do the whole thing in awk
Code: [Select]
awk 'BEGIN {total = 0}($1 == "Name"){total++} END {print total}' *.txt