Why sum of recs in awk don't match total rec count? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Why sum of recs in awk don't match total rec count?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-26-2013
mjf mjf is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 24 April 2014, 10:00 AM EDT
Location: Newtown, PA
Posts: 110
Thanks: 5
Thanked 24 Times in 23 Posts
Why sum of recs in awk don't match total rec count?

I'm using awk to determine if a field starting in position 604 for length of 10 is not equal to ALL spaces. It's searching several files which are in the current directory.

The below awk indicates that there are 84 records on all files where this field IS NOT equal to ALL spaces ( there are 10 spaces between the quotes).


Code:
 
awk 'substr($0,604,10)!="          " {print substr($0,604,10)}' LPM_BENE_BATCH_*.txt | wc -l
84

I then want to run the negate of the above to validate against the total record count of all files.
The below awk indicates that there are 444 records on all files where this field equals all spaces.


Code:
 
awk 'substr($0,604,10)=="          " {print substr($0,604,10)}' LPM_BENE_BATCH_*.txt | wc -l
444

But when I count all records on all the same files the above 2 awk commands are searching I get 1 less record then when I sum the above counts (84+444 = 528).


Code:
 
wc -l LPM_BENE_BATCH_*.txt 
527 total

What are some factors that could cause my total to not match?
Sponsored Links
    #2  
Old 02-26-2013
alister alister is offline Forum Advisor  
Registered User
 
Join Date: Dec 2009
Last Activity: 24 April 2014, 7:02 PM EDT
Posts: 3,132
Thanks: 172
Thanked 937 Times in 761 Posts
wc counts the number of newlines. If one of the files doesn't end in a newline, that would explain the discrepancy, since awk would add the missing newline when it prints that record.

Regards,
Alister
The Following User Says Thank You to alister For This Useful Post:
mjf (02-26-2013)
Sponsored Links
    #3  
Old 02-26-2013
mjf mjf is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 24 April 2014, 10:00 AM EDT
Location: Newtown, PA
Posts: 110
Thanks: 5
Thanked 24 Times in 23 Posts
Thanks for the explanation alister! I was able to find the offending line missing the newline char.

Last edited by mjf; 02-26-2013 at 08:45 PM..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sum using awk with pattern match sol_nov Shell Programming and Scripting 2 08-31-2012 03:45 AM
awk and count sum ? sabercats Shell Programming and Scripting 5 05-11-2012 10:44 PM
Total Count using AWK sraj142 Shell Programming and Scripting 8 08-16-2011 04:19 AM
awk count characters, sum, and divide by another column peromhc Shell Programming and Scripting 1 06-14-2010 06:48 PM
how to count number of rows and sum of column using awk pistachio UNIX for Dummies Questions & Answers 2 05-20-2010 12:55 AM



All times are GMT -4. The time now is 07:53 PM.