use awk to aggregrate the field number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting use awk to aggregrate the field number
# 1  
Old 08-10-2008
use awk to aggregrate the field number

Hi,

I have a various files;each filled with hundreds of line with similar number of fields.

I would like to extract out field $5 from each of this file and aggregate them before printing out to a file.

I tried to :-

Code:
#!/usr/bin/awk -f

file="file1.txt file2.txt file3.txt file4.txt"

for loop in $file;
do

while((getline < "$loop"))
{
     value+=$5;
}
print $loop $value>"../" Final.txt

done;

I execute them but it gives me error:-

awk: ./loop.bash:5: for loop in $file;
awk: ./loop.bash:5: ^ syntax error
awk: ./loop.bash:6: do
awk: ./loop.bash:6: ^ syntax error

Basically, the file1.txt, file2.txt...etc.. is something like

File1.txt
foo,apple,2,3,0.02,5,6
foo,apple,2,3,5.6,5,6
foo,apple,2,3,1.23,5,6
foo,apple,2,3,4.50,5,6
foo,apple,2,3,9.20,5,6

File2.txt
foo,apple,2,3,2.00,5,6
foo,apple,2,3,5.6,5,6
foo,apple,2,3,1.3,5,6
foo,apple,2,3,4.40,5,6
foo,apple,2,3,9.10,5,6

etc..

The output i like to see is;Final.txt will have
File1.txt 20.5 #which is the sum of the $5 field in file1.txt
File2.txt 22.4 #which is the sum of the $5 field in file2.txt

I also tried to convert the above code to bash-like by using awk'{}' but its not working either.

Please advise whether what error or silly mistakes i did made in the above code.
# 2  
Old 08-10-2008
This should work
Code:
for i in file*.txt; do awk -F',' '{a+=$5}END{print FILENAME, a >> "Final.txt"}' $i;done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to parse comma separated field and removing comma in between number and double quotes

Hi Experts, Please support I have below data in file in comma seperated, but 4th column is containing comma in between numbers, bcz of which when i tried to parse the file the column 6th value(5049641141) is being removed from the file and value(222.82) in column 5 becoming value of column6. ... (3 Replies)
Discussion started by: as7951
3 Replies

2. Shell Programming and Scripting

awk command to return only field with a number in it

What is an awk command to print only fields with a number in it?? Input file....... S,S,S,S,S,S,S,S,S 001S,S,S,S,S,S,S,S,S 00219S,23S,24S,43S,47S,S,S,S,S 00319S,10S,23S,41S,43S,47S,S,S,S 00423S,41S,43S,46S,47S,S,S,S,S 00510S,23S,24S,43S,46S,S,S,S,S 00610S,23S,43S,46S,47S,S,S,S,S... (2 Replies)
Discussion started by: garethsays
2 Replies

3. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

4. Shell Programming and Scripting

awk assign output of array to specific field-number

With this script i want to print the output to a specific field-number . Can anybody help? awk 'NR=FNR{split(FILENAME,fn,"_");nr=$2;f = $1} END{for (i=1;i<=f;i++) print i,$fn=nr}' input_5.csv input_6.csvinput_5.csv 4 135 5 185 6 85 11 30input_6.csv 1 90 3 58 4 135 7 60 8 55 10... (1 Reply)
Discussion started by: sdf
1 Replies

5. Shell Programming and Scripting

awk conditional expression to compare field number and variable value

Hi, I'm trying to compare the value in a field to the value in a variable using awk. This works: awk '$7 == "101"'but this is what I want (and it doesn't work): value=101 awk '$7 == "$value"' Any help or insight on this would be great. Thanks in advance. (1 Reply)
Discussion started by: goodbenito
1 Replies

6. Shell Programming and Scripting

Adding total of first field for each number in the second field

Dears, I need a script or command which can find the unique number from the second filed and against that number it adds the total of first field . 17215630 , 0 907043 ,1 201050 ,10 394149 ,4 1964 ,9 17215630, 0 907043 ,1 201050, 10 394149 ,4 1964 ,9 1234234, 55 23 ,100 33 ,67 ... (2 Replies)
Discussion started by: shary
2 Replies

7. Shell Programming and Scripting

Sorting on two fields time field and number field

Hi, I have a file that has data in it that says 00:01:48.233 1212 00:01:56.233 345 00:09:01.221 5678 00:12:23.321 93444 The file has more line than this but i just wanted to put in a snippet to ask how I would get the highest number with time stamp into another file. So from the above... (2 Replies)
Discussion started by: pat4519
2 Replies

8. Shell Programming and Scripting

awk to print exact field number

Hello there. I want some help with awk I have this script that reads a file from awk and you can insert from keyboard any list from the fields that exist so to be printed on monitor echo "give a choice" read -a ans array=${ans} awk -F: -v k="$array" '{ ... (4 Replies)
Discussion started by: covis
4 Replies

9. Shell Programming and Scripting

doing rot13 on a field with a random number with awk

i have a several million line file like this: M:charitygeneral:water:fairbanks:charitygeneral field 2 and field 5 are the same i want to read the file and rot13 or any caesar cipher field 2 and replace the with a random number 1 - 9 anyone know how to do this? something slightly... (8 Replies)
Discussion started by: bathtub
8 Replies

10. Shell Programming and Scripting

[awk]: Row begins by random number and field 10 is greater than 10.00%

Hello! I wish to extract the pid where CPU is above 10% last pid: 22621; load averages: 4.71, 5.04, 5.13 15:08:34 221 processes: 212 sleeping, 2 running, 1 stopped, 6 on cpu CPU states: %... (3 Replies)
Discussion started by: Lomic
3 Replies
Login or Register to Ask a Question