Question about awk command

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Question about awk command
# 1  
Old 03-27-2015
Question about awk command

hi gurus,


I am not sure what the below commands does.

Code:
 
awk 'BEGIN {sum=0} {sum=sum+$5} END {print sum}' input_file

# 2  
Old 03-27-2015
Hello ramkumar15,

Following may help you in same.
Code:
awk '
BEGIN {sum=0} ## In begining sum variable is set to 0
{sum=sum+$5}  ## Now adding $5 values each time with it's previous values so that we can get complete SUM of $5 in variable sum
END {print sum}' input_file  ## At last printing value of variable sum

Thanks,
R. Singh
# 3  
Old 03-27-2015
Quote:
Originally Posted by ramkumar15
hi gurus,


I am not sure what the below commands does.

Code:
 
awk 'BEGIN {sum=0} {sum=sum+$5} END {print sum}' input_file


Code:
awk '   # Run awk with the following script...
BEGIN {
               # variable sum is set with a value 0
               sum=0      
} 

# File reading starts from here, read line by line...

{

  # You can also use sum+=$5 it is just a shorthand/shortcut
  # What below expression does is that it first adds the value stored in variable sum to column5 value from your file,
  # and then stores the total of the two back to the variable sum

  sum=sum+$5   

} 


END{
       # After all lines have been read from all input files given to
       # this invocation of awk, run the commands in this section.

      # Finally print sum here
      print sum
}
 ' input_file  # End the script and specify the input file(s) to be processed by this


Last edited by Akshay Hegde; 03-27-2015 at 04:22 AM..
# 4  
Old 03-27-2015
This is progammed utmost meticulously, which is good practice and not a bad thing in the first place. but, using some awk- features, it could be boiled down to

Code:
awk '{sum+=$5} END {print sum}' input_file

Any awk variable is allocated on first reference and defaults to zero/empty, so the BEGIN section is superfluous. The += is the addition assignment known from e.g. C .
# 5  
Old 03-27-2015
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Question about a awk command

Hi, I am studying an awk command: awk '{ sub(/\/\/.*/, "", $NF); print }' input.txt The input.txt is: char*s1="//hello"; //comment //delete /* hello //*/ The output is : char*s1="//hello"; /* hello (2 Replies)
Discussion started by: jeffwang66
2 Replies

2. Shell Programming and Scripting

Pass awk field to a command line executed within awk

Hi, I am trying to pass awk field to a command line executed within awk (need to convert a timestamp into formatted date). All my attempts failed this far. Here's an example. It works fine with timestamp hard-codded into the command echo "1381653229 something" |awk 'BEGIN{cmd="date -d... (4 Replies)
Discussion started by: tuxer
4 Replies

3. Shell Programming and Scripting

awk question : system output to awk variable.

Hi Experts, I am trying to get system output to capture inside awk , but not working: Please advise if this is possible : I am trying something like this but not working, the output is coming wrong: echo "" | awk '{d=system ("date") ; print "Current date is:" , d }' Thanks, (5 Replies)
Discussion started by: rveri
5 Replies

4. Shell Programming and Scripting

Multiple command execution inside awk command during xml parsing

below is the output xml string from some other command and i will be parsing it using awk cat /tmp/alerts.xml <Alert id="10102" name="APP-DS-ds_ha-140018-componentFailure-S" alertDefinitionId="13982" resourceId="11427" ctime="1359453507621" fixed="false" reason="If Event/Log Level(ANY) and... (2 Replies)
Discussion started by: vivek d r
2 Replies

5. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 Replies

6. Shell Programming and Scripting

AWK printf command question

Hi, I am using below awk code to convert a csv file data into fixed file format. awk 'BEGIN { FS = "," fmt = "%10s%010d%10s%d%1d\n" } NR>1 { printf fmt, $1, $2, $3, $4*100, $5 }' /data/mydata.csv > /data/fixed.dat Data in mydata.csv ================... (2 Replies)
Discussion started by: kbmkris
2 Replies

7. UNIX for Dummies Questions & Answers

AWK printf command question

Hi, I am using below awk code to convert a csv file data into fixed file format. awk 'BEGIN { FS = "," fmt = "%10s%010d%10s%d%1d\n" } NR>1 { printf fmt, $1, $2, $3, $4*100, $5 }' /data/mydata.csv > /data/fixed.dat Data in mydata.csv ================... (1 Reply)
Discussion started by: kbmkris
1 Replies

8. Shell Programming and Scripting

awk command in script gives error while same awk command at prompt runs fine: Why?

Hello all, Here is what my bash script does: sums number columns, saves the tot in new column, outputs if tot >= threshold val: > cat getnon0file.sh #!/bin/bash this="getnon0file.sh" USAGE=$this" InFile="xyz.38" Min="0.05" # awk '{sum=0; for(n=2; n<=NF; n++){sum+=$n};... (4 Replies)
Discussion started by: catalys
4 Replies

9. UNIX for Dummies Questions & Answers

awk and find command question

Hello! I have written this script: for file in "$( find $dirName -type d )" do echo "$file" echo "hello" done but as a result I get all the directories and in the end the work "hello". Shouldn't it print the word "hello" after printing the name of each directory and not in the end? ... (1 Reply)
Discussion started by: GeorgeP
1 Replies

10. UNIX for Dummies Questions & Answers

Basic awk question...getting awk to act on $1 of the command itself

I have a script problem that I am not able to solve due my very limited understanding of unix/awk. This is the contents of test.sh awk '{print $1}' From the prompt if I enter: ./test.sh Hello World I would expect to see "Hello" but all I get is a blank line. Only then if I enter "Hello... (2 Replies)
Discussion started by: JasonHamm
2 Replies
Login or Register to Ask a Question