The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com




Thread: Script help
View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #2 (permalink)  
Old 11-01-2006
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
You are counting the lines in a file. Then you add a line. Then you display the new count. Then you add up the old counts.


Code:
$ echo "one
> two
> three" > file
$ cat file
one
two
three
$ cat file | wc -l
       3
$ print "extra total line that the above wc missed somehow"  >> file  | more file | wc -l
       4

Counting the lines in a file twice will slow you down at best. It is really bad that you recount after adding a line. And look at that pipeline. It doesn't make sense. The print statement is redirected to a file so it has nothing to feed into "more". And "more" will open a file in this case and ignore stdin anyway.

Rather than "cat file | wc -l", use just:
wc -l < file
By redirecting the input, the shell opens the file rather than wc opening the file. So wc will not display the filename. Try to do this once and use the same count in both places.