Script explanation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script explanation
# 1  
Old 06-11-2005
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  
Old 06-11-2005
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  
Old 06-11-2005
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  
Old 06-11-2005
[A-Z]* , the * means zero or more occurrences.
# 5  
Old 06-12-2005
Thanks a lot for your help
# 6  
Old 06-13-2005
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?
# 7  
Old 06-13-2005
Becuase the search patter does not affect the number of records, it is only used to decide on which records to perform the actions. The number of records it the number of lines in the input file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script explanation

Hey, can someone explain me this script? i=0 while read WORT do echo $WORT|grep a>/dev/null || echo$WORT|grep B>dev/null || let i=$i+1 done echo $i The first lane initializie the variable i with the value of 0. The loop line has 3 different options because of ||. The only option I... (10 Replies)
Discussion started by: newuser21
10 Replies

2. Shell Programming and Scripting

What does this shell script do? Need in depth explanation please

Hi My friend wrote this particular script and won't tell me what it does, and when I run it I don't understand it. What does the entire script do with specifics please. Thanks Bob #!/bin/bash current=0 while ; do if ; then echo ${current} current=$((${current}+1)) fi done (1 Reply)
Discussion started by: shadowknight777
1 Replies

3. Shell Programming and Scripting

Explanation of FNR in this awk script

To merge mutiple *.tab files as: file1.tab rs1 A A rs2 A A rs3 C C rs4 C Cfile2.ind rs1 T T rs2 T T rs3 G G rs4 G Gand file3.tab rs1 B B rs2 B B rs3 L L rs4 L LOutput: file1.tab file2.tab file3.tab AA TT BB AA TT BB CC GG LL CC GG ... (4 Replies)
Discussion started by: yifangt
4 Replies

4. Solaris

Explanation of script

Hello Guys, can someone help explain the script below for me? I will really appreciate it. vi db_script #!/bin/sh echo .cron job run on.`date`> cronjob.txt df -h >> cronjob.txt echo welcome to home (2 Replies)
Discussion started by: cjashu
2 Replies

5. Shell Programming and Scripting

need an explanation on this script...

The following script will create a directory in a directory and will go on as many times as the number you will give in. I am trying to find out how it works ... can someone please help me with that? #!/bin/sh #create a variable and set it to 1 n=1 #start a loop as... (3 Replies)
Discussion started by: I-1
3 Replies

6. Shell Programming and Scripting

Script explanation

#script fileused=test.txt hostname=test.dis.com ftp $hostname <<-! > $fileused.err 2>&1 put file.txt /usr/text.txt bye ! kindly the above script the one marked as Bold and underlined as the above i am declaring the new variable as filename ,But when i used i had used as $fileused.err... (1 Reply)
Discussion started by: rajar_r
1 Replies

7. Shell Programming and Scripting

Shell Script Explanation

Hello, I have seen this script on this site. I understand most of it. However I am a bit stuck on the part in red. It appears to be expanding for file in *.zip do zipdir=${file%.*} mkdir $zipdir || echo "unable to create $zipdir" cp $file $zipdir || echo "unable to copy $file"... (3 Replies)
Discussion started by: jaysunn
3 Replies

8. Shell Programming and Scripting

explanation for a script

Guys, was wondering what the meaning of the below bit is ? awk -F ' ' '{print $1 " " $2 ;}' $TEMPFILE | (rm -f $TEMPFILE; sed 's/$/ '"$box"'/g' > $TEMPFILE) Can anyone explain this in detail? what is the significance of rm -f $TEMPFILE here? What all IO/buffering happens here ?How the... (0 Replies)
Discussion started by: hashin_p
0 Replies

9. Shell Programming and Scripting

Need explanation of script

Hi All, Can anybody explain what this script is doing? #!/bin/sh who | cut -d " " -f1 | sort -u > userlist1 while true ; do sleep 60 who | cut -d" " -f1 | sort -u >userlist2 for username in `cat userlist1` ; do if ! grep "^$username$" userlist2 > /dev/null ; then echo... (0 Replies)
Discussion started by: vishalpatel03
0 Replies

10. Shell Programming and Scripting

Explanation of running this script

I have a script that has defined a log file like this. The name of the script is verify.sh Inside the script there is some thing like this. LOG=/usr/verify TDATE=`date "+%m%d%y"$$` LOGFILE=$LOG.$TDATE. and inside the script it has been written as echo "This is to verify" | tee -a... (2 Replies)
Discussion started by: sendhilmani
2 Replies
Login or Register to Ask a Question