September 2010
M T W T F S S
« Apr    
 12345
6789101112
13141516171819
20212223242526
27282930  

Shell Scripts: using wc and grep and awk

1. count the code lines using wc.

   1: wc -l `find . -name *.c` 

2. count the code lines, using an elegant way "xargs" to transfer the arguments of wc

   1: find . -name *.c |xargs wc -l 

3. count the code lines, self calculation the result of wc using awk

   1: find . -name *.c |xargs wc -l|awk '
   2: BEGIN {sum=0;fnum=0;}
   3: {sum=sum+$1; printf "%-10s %s\n",$1,$2; fnum=fnum+1; }
   4: END {printf "Total files: %s,\tTotal lines: %s.\n",fnum,sum} 
   5: '

4. List all the text files residing in current directory, recursively,

[code='c']

grep -rIc “.*” . | awk -F : ‘{if($2!=0)print($1)}’

[/code]

Here, -I let grep only search text files, -c count the line of files. When the count is larger than 0, we print it out.

2 comments to Shell Scripts: using wc and grep and awk

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>