Loop through file to sum conditionally


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop through file to sum conditionally
# 1  
Old 05-10-2011
Loop through file to sum conditionally

Hi,

I have a file with header, detail and trailer records.

HDR|111
DTL|abc|100|xyz
DTL|abc|50|xyz
TRL|150

I need to add the values in 3rd field from DTL records.

Using awk, I am doing it as follows:

awk -F'|' '$1=="DTL"{a += $3} END {print a}' <source_file>

However, I want to be able to pass the field number as a parameter (above in red) so I can use this script for different files.

How can I achieve this?

Thanks for the help.


Last edited by delta21; 05-10-2011 at 10:29 PM.. Reason: Fix the code snippet
# 2  
Old 05-10-2011
This will work with modern versions of awk. Use nawk if you're on a Sun.

Code:
awk -v col=$col -F'|' '$1=="DTL"{ a += $(col) } END {print a}'

This assumes that $col is defined in your script as the column you want to dig from.
# 3  
Old 05-10-2011
Alternatively, you can insert the shell variable there directly; turning the single quotes on and off:
Code:
col=3
awk -F'|' '$1=="DTL"{a += $'$col'} END {print a}'

# 4  
Old 05-11-2011
Thanks folks! I tried both ways, and it worked.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help the sum from loop

Hi all, I have one host i need to run in loop to check the capacity from different frame and get the output to one file and sum it and convert to TB this is Code #!/bin/ksh DATE=`date '+%d%m%y'` for f in `cat /home/esx-capacity/esx-host.txt` do for g in `cat /home/esx-capacity/frame`... (10 Replies)
Discussion started by: ranjancom2000
10 Replies

2. Post Here to Contact Site Administrators and Moderators

awk to sum in Loop

i want code in awk with loop to get the sum * is delimiter in file TOTAL_AMOUNT=SUM(CLP04) suppose there are 12 CLP04 segment in my file i want to add upto 5 CLP04 then print next line after BPR segment after calculate the total amount CLP04 means ex ... (5 Replies)
Discussion started by: MOHANP12
5 Replies

3. Shell Programming and Scripting

For loop to get rid of first 2 lines(conditionally)

hello all, I get data from different vendors and need to clean it up. Usually it pretty straight forward when i have files that only have headers....but in my case i have files that have a starting line of file name(and some junk info) and 2nd line is headers and the 3rd line is were the actual... (11 Replies)
Discussion started by: crazy_max
11 Replies

4. Shell Programming and Scripting

Sum up numbers in a for loop

Hi i have to calculate some numbers, column by column. Herfore i used a for-loop.. for i in {4..26};do awk -F"," '{x'$i'+=$'$i'}END{print '$i'"\t" x'$i'}' file.tmp;done ----- printout ----- 4 660905240 5 71205272 6 8.26169e+07 7 8.85961e+07 8 8.60936e+07 9 7.42238e+07 10 5.6051e+07... (7 Replies)
Discussion started by: IMPe
7 Replies

5. Shell Programming and Scripting

For Loop & SUM

pcmpath query device |awk 'BEGIN{print "DEVICE NAME\tSERIAL"} /DEVICE NAME/ {printf "%s\t", $5; getline; print substr($2, length($2)-3)}' This script returns output like below to pull out "DEVICE NAME SERIAL". ...... hdisk28 110B hdisk29 1112 hdisk30 1115 hdisk31 1116 hdisk32 1128... (2 Replies)
Discussion started by: Daniel Gate
2 Replies

6. Shell Programming and Scripting

loop + sum + print using awk

Hi, I am unable sum of each column in the loop usng awk command. Awk is not allowing the parameters in the command. i am facing the below error. awk: 0602-562 Field $() is not correct. Source file abc.txt 100,200,300,400,500,600,700,800,900 101,201,301,401,501,601,701,801,901 ... (1 Reply)
Discussion started by: number10
1 Replies

7. Shell Programming and Scripting

perl - reading from a file conditionally

Hi, I am new to perl. I want to read from a file on the basis of some conditions.. I want to define parameters in a configuration file in such a manner like... etc.. in my perl script, theer is a variable like this.. then i want to read values from first if block from the file... (1 Reply)
Discussion started by: shellwell
1 Replies

8. Shell Programming and Scripting

While loop - The sum seems to be local

Hi, I am trying to extracting the sum of all varibles listed in a file. The code is as follows ##### FILE1 ######## Value1:2 Value2:2 Value3:6 Value4:5 ##### shell script ###### #!/bin/sh total=0 (2 Replies)
Discussion started by: eagercyber
2 Replies

9. Shell Programming and Scripting

How to update the contents in a file conditionally?

Hi All, I have a data file which has two columns Location and the Count. The file looks like this India 1 US 0 UK 2 China 0 What I have to do is whenever I fails to login to Oracle then I have to add 1 to the count for that location. Whenever my script fails to login to Oracle for a... (5 Replies)
Discussion started by: rajus19
5 Replies
Login or Register to Ask a Question