Nawk, creating a variable total from multiple lines(records)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nawk, creating a variable total from multiple lines(records)
# 1  
Old 04-14-2011
Nawk, creating a variable total from multiple lines(records)

Good Morning/Afternoon All,

I am having some trouble creating a variable called "total" to display the sum of the values in a specific field, $6 for example.

The data I am working on is in the following form:

Code:
John Doe:(555) 555-5555:1:2:3
Jane Doe:(544) 444-5556:4:5:6
Moe Doe:(654) 333-9999:7:8:9

I am using ":" as the field separator in order to have a total of 5 fields.
I then add the values in fields 3,4,5 to generate field 6
Code:
John Doe:(555) 555-5555:1:2:3:6
Jane Doe:(544) 444-5556:4:5:6:15
Moe Doe:(654) 333-9999:7:8:9:24

Now I want to add all the values in field 6 to generate a single total for the entire file, this is where I am stuck.

I currently have the following for my nawk script
Code:
nawk -F: '{split($0,six,":"); total=(six[3]+six[4]+six[5]); print total, NF}' filename

To my understanding, the above code creates an array "six" which stores all the values for each record(line) while using ":" as the separator.

I am thinking of storing the values in field 6 to a separate array called "dtotal" then doing a sum for all values in dtotal to give me the overall total for the file because I also need to display an average and highest value for field 6.

Any and all help is greatly appreciated.
Thanks in Advance.

Last edited by SEinT; 04-14-2011 at 08:32 AM.. Reason: Please use code tags
# 2  
Old 04-14-2011
Something like this?
Code:
awk -F: '{$6=$3+$4+$5;total+=$6}END{print "Total = " total}1' OFS=":" file

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 04-14-2011
Quote:
Originally Posted by Franklin52
Something like this?
Code:
awk -F: '{$6=$3+$4+$5;total+=$6}END{print "Total = " total}1' OFS=":" file

Thank you very much.
I spent the last couple of hours trying to do this with a for loop Smilie
Many many thanks once again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Performance of calculating total number of matching records in multiple files

Hello Friends, I've been trying to calculate total number of a certain match in multiple data records files (DRs). Let say I have a daily created folders for each day since the beginning of july like the following drwxrwxrwx 2 mmsuper med 65536 Jul 1 23:59 20150701 drwxrwxrwx 2 mmsuper... (1 Reply)
Discussion started by: EAGL€
1 Replies

2. Shell Programming and Scripting

Creating multiple xml tag lines in a file

Hi All, Can someone tell me how can we create same xml tag lines based on the number of lines present in other file and replace the Name variable vaule present in other file. basically I have this xml line <typ:RequestKey NameType="RIC" Name="A1" Service="DDA"/> and say I... (4 Replies)
Discussion started by: Optimus81
4 Replies

3. Shell Programming and Scripting

problem in redirecting records using nawk

I am trying to redirect record to two files using nawk if-else. #Identify good and bad records and redirect records using if-then-else nawk -F"|" '{if(NF!=14){printf("%s\n",$0) >> "$fn"_bad_data}else{printf("%s\n",$0) >> $fn}}' "$fn".orig "$fn".orig is the source file name bad... (7 Replies)
Discussion started by: siteregsam
7 Replies

4. Shell Programming and Scripting

how to Insert values in multiple lines(records) within a pipe delimited text file in specific cols

this is Korn shell unix. The scenario is I have a pipe delimited text file which needs to be customized. say for example,I have a pipe delimited text file with 15 columns(| delimited) and 200 rows. currently the 11th and 12th column has null values for all the records(there are other null columns... (4 Replies)
Discussion started by: vasan2815
4 Replies

5. Shell Programming and Scripting

Nawk help searching for multiple lines and multiple searches

I use this command to find a search (Nr of active alarms are) and print one line before and 10 lines after the search keywords. nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r;print;c=a}b{r=$0}' b=1 a=10 s="Nr of active alarms are:" *.log However, I would like to know how to tell it to print... (3 Replies)
Discussion started by: tthach830
3 Replies

6. Shell Programming and Scripting

Total records in table

Hi, I am having two tables. A & B I want to know the total number of records in each table and need to store each value in some variables to process further in the code. How it can be done ? With Regards (2 Replies)
Discussion started by: milink
2 Replies

7. Shell Programming and Scripting

Extract CSV records using NAWK?

Example CSV: $ cat myfile HDR COL_A,COL_B,COL_C X,Y,Z Z,Y,X ... X,W,Z In this example, I know that column names are on the second line. I also know that I would like to print lines where COL_A="X" and COL_C="Z". In this simple example, I know that COL_A = $1 and COL_C = $3, and hence... (6 Replies)
Discussion started by: cs03dmj
6 Replies

8. Shell Programming and Scripting

Logfile parsing with variable, multiple criterias among multiple lines

Hi all I've been working on a bash script parsing through debug/trace files and extracting all lines that relate to some search string. So far, it works pretty well. However, I am challenged by one requirement that is still open. What I want to do: 1) parse through a file and identify all... (3 Replies)
Discussion started by: reminder
3 Replies

9. UNIX for Dummies Questions & Answers

Help in Array looping and creating multiple lines

hi Gurus, I'm a newbie in scripting please check my script if this is correct. I think there's something wrong with it but I;m not sure. I'm trying to create multiple lines using awk from external xml files but i want to add additonal info in the data manually Since i don't knwo how to... (0 Replies)
Discussion started by: sexyTrojan
0 Replies

10. UNIX for Dummies Questions & Answers

grep running total/ final total across multiple files

Ok, another fun hiccup in my UNIX learning curve. I am trying to count the number of occurrences of an IP address across multiple files named example.hits. I can extract the number of occurrences from the files individually but when you use grep -c with multiple files you get the output similar to... (5 Replies)
Discussion started by: MrAd
5 Replies
Login or Register to Ask a Question