The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-10-2008
ahjiefreak ahjiefreak is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 132
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 (permalink)  
Old 08-10-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,428
This should work
Code:
for i in file*.txt; do awk -F',' '{a+=$5}END{print FILENAME, a >> "Final.txt"}' $i;done
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:02 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0