Calculating field using AWK, or GAWK script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Calculating field using AWK, or GAWK script
# 1  
Old 10-28-2007
Question Calculating field using AWK, or GAWK script

Hello all,

I'm totally new to UNIX/Linux but I'm taking a course in it at my local JC.

My question: I have been tasked with writing a gawk script that will create a nicely formatted report. That part I've done ok on...however, the very last thing that must be done is a calculation of a particular field of data. Here is my script:

BEGIN { printf("%s%12s%10s%10s\n", "Date", "Low", "High", "Rain")
printf("=====================================\n") }
{ printf("8s%8s%10s%10s\n", $2, $4, $7, $11) }

END { printf"=====================================\n")
printf("Total Rain = %d\n", NR) }

I need the last line of code to calculate field $11 which should come out to 2.5 inches of rain based on my data file. I know right now that the last line of code is only calculating the number of records.

Any ideas?

Thanks in advance!

Trellot Smilie
# 2  
Old 10-28-2007
Code:
BEGIN { printf("%s%12s%10s%10s\n", "Date", "Low", "High", "Rain")
printf("=====================================\n") }
{ printf("8s%8s%10s%10s\n", $2, $4, $7, $11); totRain+=$11}

END { printf"=====================================\n")
printf("Total Rain = %d\n", totRain) }

# 3  
Old 10-28-2007
Quote:
Originally Posted by vgersh99
Code:
BEGIN { printf("%s%12s%10s%10s\n", "Date", "Low", "High", "Rain")
printf("=====================================\n") }
{ printf("8s%8s%10s%10s\n", $2, $4, $7, $11); totRain+=$11}

END { printf"=====================================\n")
printf("Total Rain = %d\n", totRain) }

Awesome! Thanks Vgersh, it works great now! WoW, what a great forum!! Glad I joined.

TrellotSmilieSmilie
# 4  
Old 10-28-2007
it's time to hit the books....
# 5  
Old 10-28-2007
Quote:
Originally Posted by vgersh99
it's time to hit the books....
Most definitely...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Gawk field separator problem

I'm trying to run a code using gawk and I'm having some trouble with it. What I'm trying to do is pull out value11 from the following input: value11,value12,value13 value21,value22,value23 I have successfully done this before using awk with the following code: awk 'NR == 1 {FS=",";... (4 Replies)
Discussion started by: KomjongShawn
4 Replies

2. Shell Programming and Scripting

Calculating the epoch time from standard time using awk and calculating the duration

Hi All, I have the following time stamp data in 2 columns Date TimeStamp(also with milliseconds) 05/23/2012 08:30:11.250 05/23/2012 08:30:15.500 05/23/2012 08:31.15.500 . . etc From this data I need the following output. 0.00( row1-row1 in seconds) 04.25( row2-row1 in... (5 Replies)
Discussion started by: ks_reddy
5 Replies

3. Windows & DOS: Issues & Discussions

Gawk on Windows: Joining lines only if 1st field matches

Hi.. i have two files:: file_1:: mOnkey huMAnfile_2:: Human:hates:banana i:like:*** Monkey:loves:banana dogs:kill:catsdesired output:: Monkey:loves:banana Human:hates:bananaso only when the 1st field matches from both files print it from file_2 ((case-sensitive)) i also would like... (21 Replies)
Discussion started by: M@LIK
21 Replies

4. Shell Programming and Scripting

AWK way of calculating growth

Hi All, IS there any 'awk' way to manipulate following data? Fruit Date Count Apple 20/08/2011 5 Apple 27/08/2011 7 Apple 05/09/2011 11 Apple 12/09/2011 3 Apple 19/09/2011 25 . . . . Orange 20/08/2011 9 Orange 27/08/2011 20 Orange 27/08/2011 7 Orange 05/09/2011 15 Orange... (3 Replies)
Discussion started by: aniketdixit
3 Replies

5. Shell Programming and Scripting

Calculating number of records by field

Hi, I have CSV file which looks like below, i want to calulate number of records for each brand say SOLO_UNBEATABLE E and SOLO_UNBEATABLE F combined and record count is say 20 . i want to calculate for each brand, and here only first record will have all data and rest of record for the brand... (2 Replies)
Discussion started by: raghavendra.cse
2 Replies

6. Shell Programming and Scripting

gawk help for inserting a field of a .txt file in the same file

i had the following type of data file vchrdump: Vouchers For Date :05/01/2009 * ... (4 Replies)
Discussion started by: KANNI786
4 Replies

7. Shell Programming and Scripting

awk script to split field data

Hi Experts, I have a Input.txt document which contains data fields seperated by tabs. There are 4 fields totally Named UNIQUE, ORDER, CONTACT and WINS. The UNIQUE field contains unique ID and the CONTACT field contains data seperated by comma in some records. I am looking to write an awk script... (12 Replies)
Discussion started by: forumthreads
12 Replies

8. Shell Programming and Scripting

field seperator question (awk script)

Is there a way I could use different a different field seperator for different parts of the body? kinda like {FS = ":"} FILENAME == "products"{ price = $3 if(numprods < $1-100) numprods = $1-100 } {FS = "/"}{} FILENAME == "associates"{ associateid... (5 Replies)
Discussion started by: angermanaged
5 Replies

9. Shell Programming and Scripting

gawk multiply one field

Hi, I am a newbie to gawk and I really don't know how to do this... I have a file with several columns of numbers and I want to make operations with some of the values... cat file.dat | gawk -v NAME=2 '/AS/ {TIME=$4} $1==NAME {print TIME,($3*1)}' The result of multiplying the field 3 by... (2 Replies)
Discussion started by: pau
2 Replies

10. Shell Programming and Scripting

Split a field in awk script

Hi all, I have a field in the line, let's say argument $6, which is in the format 00.00 If i want to split the field to get rid of the "." in between of the amount, how can i do that i awk script? I have it like this split($6,a,".") but it will get rid of the last 2 digits after the... (4 Replies)
Discussion started by: CamTu
4 Replies
Login or Register to Ask a Question