Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


File Row Line Count without Header Footer


 
Thread Tools Search this Thread
# 1  
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  
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  
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  
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  
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
Is there a way to append both at header and footer of a file
jediwannabe
currently I've a file Insert into CD_CARD_TYPE (CODE, DESCRIPTION, LAST_UPDATE_BY, LAST_UPDATE_DATE) Values ('024', '024', 2, sysdate); Insert into CD_CARD_TYPE (CODE, DESCRIPTION, LAST_UPDATE_BY, LAST_UPDATE_DATE) Values ('032', '032', 2, sysdate); ........ is it...... Shell Programming and Scripting
3
Shell Programming and Scripting
Removing header or footer from file
sridhardwh
Hi Every one, what is the coomand to remove header or footer from a file. Please help me by providing command/syntax to remove header/footer from unix. Thanks in advance for all your support.... Shell Programming and Scripting
5
Shell Programming and Scripting
Add header and footer with record count in footer
itsranjan
This is my file(Target.txt) name|age|locaction abc|23|del xyz|24|mum jkl|25|kol The file should be like this 1|03252012 1|name|age|location 2|abc|23|del 2|xyz|24|mum 2|jkl|25|kol 2|kkk|26|hyd 3|4 Column 1 is row indicator for row 1 and 2, column indicator is 1,for data rows...... Shell Programming and Scripting
1
Shell Programming and Scripting
sort a report file having header and footer
suryanarayana
I am having report file with header and footer . The details in between header and footer are separated by a pipe charater. I want to sort the file by considering multiple columns in between header and footer. pls help... Shell Programming and Scripting
4
Shell Programming and Scripting
Append Spaces At end of each line Leaving Header and Footer
deepam
How to append constant No of spaces suppose 52 at end of each line in a file (xyz) excluding first and last line. Please Help me out for the same.... Shell Programming and Scripting
1
Shell Programming and Scripting

Featured Tech Videos