The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need explanation of script vishalpatel03 Shell Programming and Scripting 0 11-25-2007 05:55 PM
tr explanation please convenientstore Shell Programming and Scripting 1 05-31-2007 12:42 AM
Explanation of running this script sendhilmani Shell Programming and Scripting 2 03-21-2006 03:07 AM
AWK explanation penfold Shell Programming and Scripting 2 04-05-2005 10:46 PM
any explanation for thsi shell script behaviour xiamin Shell Programming and Scripting 9 11-09-2001 01:13 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-11-2005
sickboy sickboy is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 62
Script explanation

I have the following script


Code:
awk '$1 ~ /^[A-Z]*[0-9]+/ {
        s += $NF;
        m++
    }
    END {
        print NR, m, s
}

and I use it to get results from the following file

A4792 4
COMP9021 5
K9 7
ABC 8
924 1
R2D2 3
6
JQL-636 2

I was expecting to get as result 8 3 16
but ir gives me 9 6 26

Can somebody explain me why firstly NF gives 9 instead of 8 when there are 8 records in the file. What does the red + means and generally why I have these results.

Please help me, I am new in the scripting art and I want understand to go a step further. Cheers
  #2 (permalink)  
Old 06-11-2005
reborg's Avatar
reborg reborg is online now Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,245
awk '$1 ~ /^[A-Z]*[0-9]+/ ... search for lines where the first field is either letter(s) followed by number(s) or only number(s)
s += $NF; ...add the value of the last field to variable s
m++ ... post increment counter m
}
END {
print NR, m, s ...print the number of fields, the value of the counter and the value of variable s (which is the sum of matching fields)
}

Why do you get 9 and not 8 records? Most likely beacuse the file ends with a blank line.

The + means one or more occurrences of the previous term in the regular expression.
  #3 (permalink)  
Old 06-11-2005
sickboy sickboy is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 62
Quote:
Originally Posted by reborg
awk '$1 ~ /^[A-Z]*[0-9]+/ ... search for lines where the first field is either letter(s) followed by number(s) or only number(s)
Can you explain me which symbol implies "or only number" 'cause I think that drove me in the wrong results
  #4 (permalink)  
Old 06-11-2005
reborg's Avatar
reborg reborg is online now Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,245
[A-Z]* , the * means zero or more occurrences.
  #5 (permalink)  
Old 06-12-2005
sickboy sickboy is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 62
Thanks a lot for your help
  #6 (permalink)  
Old 06-13-2005
sickboy sickboy is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 62
Sorry but still I have a question.
With your explanation it shouldn't count the line with the empty field $1 (it was misstyped and couldn't been seen the <tab> so the line was <tab> 6) and the R2D2<tab>3. But it count's them. Why?
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:25 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0