![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| count the number of field in a string with deliminter | yang | UNIX for Dummies Questions & Answers | 9 | 07-25-2008 06:24 AM |
| How to extract elements in a field using a number | ahjiefreak | Shell Programming and Scripting | 17 | 12-13-2007 04:13 AM |
| Moving Part of a field to another field using AWK | rjsha1 | Shell Programming and Scripting | 5 | 08-04-2006 06:39 AM |
| add increment field when first field changes | azekry | Shell Programming and Scripting | 2 | 11-14-2005 04:21 PM |
| [awk]: Row begins by random number and field 10 is greater than 10.00% | Lomic | Shell Programming and Scripting | 3 | 03-18-2005 09:59 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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;
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. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|