File Row Line Count without Header Footer


 
Thread Tools Search this Thread
# 1  
Old 05-02-2013
File Row Line Count without Header Footer

Hi There!

I am saving the file count of all files in a directory to an output file using:

wc -l * > FileCount.txt

I get:

114 G4SXORD
3 G4SXORH
0 G4SXORP
117 total

But this count includes header and footer. I want to subtract 2 from the count and get

112 G4SXORD
1 G4SXORH
0 G4SXORP
113 total

Is there a way to do that in a one-liner? How do I subtract 2 from wc -l output before writing it to output file?

Thanks
# 2  
Old 05-02-2013
awk is great for simple off-the-cuff things like this. Testing on a bunch of 3-line files:

Code:
wc -l * | awk '$2=="total" { $1-=A; print ; next } { $1-=2; A+=2; print }'

1 a1c
1 a2c
1 a3c
1 a4c
4 total

$

For lines with 'total' in them, it subtracts the A variable from the first column, prints, and skips to the next line.

Otherwise, it skips that and executes the next section, which subtracts 2 from the first column, adds 2 to the A variable, then prints.
# 3  
Old 05-02-2013
try this one !
wc -l file | awk '{print $1-2}'

it gives number lines-2

---------- Post updated at 11:54 AM ---------- Previous update was at 11:54 AM ----------

try this one !
wc -l file | awk '{print $1-2}'

it gives number lines-2
# 6  
Old 05-02-2013
Thanks what about 0?

Awesome Guys!

wc -l * | awk '{print $1-2 " " $2}'

and

wc -l * | awk '$2=="total" { $1-=A; print ; next } { $1-=2; A+=2; print }'

both work great

but what about 0 row count? It shows -2

Is it possible to include in there - if count < 0 then 0?

Thanks again!
# 7  
Old 05-02-2013
Code:
wc -l * | awk '!/total/{A[$2]=$1}/total/{for(k in A) {if(A[k]) s+=A[k]-2;print A[k]?A[k]-2:A[k],k} print s," total"}'

 

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Split large xml into mutiple files and with header and footer in file karthik Shell Programming and Scripting 36 20 Hours Ago 01:33 AM
Eliminate Header and footer from EBCDIC file abhilashnair UNIX for Dummies Questions & Answers 4 12-18-2014 05:48 AM
Compare file1 header count with file2 line count pone2332 Shell Programming and Scripting 4 02-26-2014 03:37 PM
Is there a way to append both at header and footer of a file jediwannabe Shell Programming and Scripting 3 02-28-2013 06:57 AM
Removing header or footer from file sridhardwh Shell Programming and Scripting 5 06-04-2012 06:43 AM
Add header and footer with record count in footer itsranjan Shell Programming and Scripting 1 03-25-2012 12:45 AM
Header and Footer... suresh_target Shell Programming and Scripting 1 02-15-2012 04:44 AM
sort a report file having header and footer suryanarayana Shell Programming and Scripting 4 11-25-2011 10:48 PM
Ignore Header and Footer and Sort the data in fixed width file sasikari Shell Programming and Scripting 5 07-14-2011 12:42 PM
Help with the Header and Footer check Sunny_teotia UNIX for Dummies Questions & Answers 5 06-06-2011 12:14 AM
Append Spaces At end of each line Leaving Header and Footer deepam Shell Programming and Scripting 1 10-21-2008 05:08 AM
Split large file and add header and footer to each small files ashish4422 Shell Programming and Scripting 7 07-07-2008 03:13 PM
Split large file and add header and footer to each file ashish4422 Shell Programming and Scripting 1 04-15-2008 07:12 AM
Total of lines w/out header and footer incude for a file gzs553 Shell Programming and Scripting 1 11-16-2006 07:42 PM
Need to Chop Header and Footer record from input file coolbudy Shell Programming and Scripting 4 08-09-2005 01:26 PM