You are not logged in.
Pages: 1
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
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
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
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
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
What's wrong with
awk '{print $8, $6, FILENAME}' *
?
Offline
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.
Offline
Pages: 1