You are not logged in.

#1 2008-12-15 17:09:08

jnwebb
Member
Registered: 2008-07-07
Posts: 52

Quick awk ?

I have a folder w/ several text files.  I want to take out two columns from each file and copy the filename at the end of each line

#####    #######   filename1
#####    #######   filename1
#####    #######   filename1
#####    #######   filename1
#####    #######   filename2
#####    #######   filename2
#####    #######   filename2
#####    #######   filename2

I fed all the filenames into an array and am now trying the following:

for ((i=0;i<${#five[@]};i++)); 
do 
awk '{print $8 "\t" $6 "\t" '${five[$i]}'}' ${five[$i]}; 
done

Thanks

Offline

#2 2008-12-15 17:28:34

tam1138
Member
Registered: 2007-09-10
Posts: 238

Re: Quick awk ?

Please clarify: what do you mean by "take out" and "copy the filename" ?  You mean remove the first two columns and duplicate the last one?  Do all files contain only three columns?  Do you want all files processed by a single awk command, or are you okay with wrapping shell scripting around awk to make that easier?  (Following the "do one thing and do it well" tenet of UNIX, I prefer the idea of writing an awk script to process a single file, and then using sh to invoke that script on each file in turn.)

Offline

#3 2008-12-15 17:34:18

jnwebb
Member
Registered: 2008-07-07
Posts: 52

Re: Quick awk ?

all files have 10 columns of data.  I want to 'take out' the 8th column (frequency) and the 6th column (acceleration) from each file and then add the filename at the end of each line of the data from that file so that I have something like the following:

0.1000    0.04883     filename#1
0.1023    0.05446     filename#1
0.1047    0.05889     filename#1
0.1072    0.06217     filename#1
0.1096    0.06315     filename#1
0.1122    0.06709     filename#1
0.1148    0.07481     filename#2
0.1175    0.08191     filename#2
0.1202    0.08492     filename#2
0.1230    0.08614     filename#2
0.1259    0.09010     filename#2
0.1288    0.09300     filename#2
0.1318    0.09763     filename#3
0.1349    0.10336     filename#3
.
.
.

would prefer in one command but ok if not....thanks

Offline

#4 2008-12-15 17:56:49

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Quick awk ?

What's wrong with the code from your original post?

I haven't tested it out, but could it be it's just missing double quotes around the last argument of the print statement?

I'd try this: (with simpler for loop)

for filename in ${five[@]}; 
do 
awk '{print $8 "\t" $6 "\t" "'$filename'"}' $filename; 
done

edit: forgot two $

Last edited by Procyon (2008-12-15 17:59:59)

Offline

#5 2008-12-15 17:58:21

tam1138
Member
Registered: 2007-09-10
Posts: 238

Re: Quick awk ?

And you want all this output in a single file?

If so, try this:

#!/bin/sh

for i in $*; do
    awk "{ print \$4 \" \" \$6 \" $i\" }" $i
done

This does assume no spaces in filenames.

Offline

#6 2008-12-16 19:42:39

briest
Member
From: Katowice, PL
Registered: 2006-05-04
Posts: 468

Re: Quick awk ?

What's wrong with

awk '{print $8, $6, FILENAME}' *

?

Offline

#7 2008-12-17 19:19:38

tam1138
Member
Registered: 2007-09-10
Posts: 238

Re: Quick awk ?

briest wrote:

What's wrong with

awk '{print $8, $6, FILENAME}' *

?

The problem with that command is that it is too advanced for my nascent awk-fu. smile

Offline

Board footer

Powered by FluxBB