Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

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

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-26-2013
mjf mjf is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 24 April 2015, 6:59 PM EDT
Location: Newtown, PA
Posts: 151
Thanks: 9
Thanked 37 Times in 35 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 Unix and Linux 02-26-2013
alister alister is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 11 June 2014, 8:40 PM EDT
Posts: 3,231
Thanks: 179
Thanked 974 Times in 789 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 Unix and Linux 02-26-2013
mjf mjf is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 24 April 2015, 6:59 PM EDT
Location: Newtown, PA
Posts: 151
Thanks: 9
Thanked 37 Times in 35 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 12:15 AM.