awk multiple column into single column
Dear all,
I'm new to using awk or similar commands and I hope someone will be able to help me. I have a multicolumn datas, like a1 b1 ... f1 a2 b2 ... f2 . . ... . . . ... . . . ... . an bn ... fn I would like to make a file with all these data in one column, like a1 a2 . . . an b1 b2 . . . bn . . . f1 f2 . . . fn Can it be done with awk or some other command? Also, is it possible then do add another column in front of this one with numbers of the lines (for every previous column), like 1 a1 2 a2 . . . . . . n an 1 b1 2 b2 . . . . . . n bn . . . . . . 1 f1 2 f2 . . . . . . n fn Thank you all in advance! Igor Lukacevic |
cat <filename> | awk '{print "1 "$1}' > <newfile>
cat <filename> | awk '{print "1 "$2}' >> <newfile> repeat for the amount of columns. You could automate this a little by putting it into a script which works out how many columns are in the file, then repeats the above line until the columns are finished. Stuart. |
You can also do this within awk, you could make nested for loops using the NR and NF variables to go through and print all the columns. See:
http://www.grymoire.com/Unix/Awk.html |
A little more tricky solution without awk:
Code:
eval cat $(seq -f "<(cut -d' ' -f%.0f file)" 1 N) |
To stuart_cherrington:
Thanks for the tip! It nicely brings them all into one column. But it adds only number 1 to each row, and not the line numbers 1,...,n (as I draw in my first post). Can this be amended? To colucix: Thanks for the tip! But your command line just gives me a blank terminal window. Nothing else. To H_TeXMeX_H: Thanks for the tip! I have a little trouble applying NR and NF loops. I need more time to see if it works. Thank you all again! Igor |
Well I will take the awk challenge :)
Code:
awk '{for(i=1;i<=NF;i++)if(arr[i] ~ /./)arr[i]=arr[i]"\n"$i;else arr[i]=$i}END{for(x=1;x<=length(arr);x++)printf("%s\n",arr[x])}' in_file |
For me the solution really depends on the total number of lines or at least the average. Also the problem tastes like C though.
|
Quote:
|
Worked just fine for me ... thanx for the lesson as always colucix :)
|
Quote:
Anyway, I used a mix of solution fromstuart_cherrington and H_TeXMeX_H and it worked just as I want it. The only thing I still lack is how to automatize several command with one loop. I tried with for loop, but failed. Here are the shell scripts I have. This one works (there are only 6 columns in the original file, so it's not a problem): #!/bin/sh cat trf2_5_band2eps.freq | awk '{print NR,$1}' > trf2_5_band2eps_edit3.freq cat trf2_5_band2eps.freq | awk '{print NR,$2}' >> trf2_5_band2eps_edit3.freq cat trf2_5_band2eps.freq | awk '{print NR,$3}' >> trf2_5_band2eps_edit3.freq cat trf2_5_band2eps.freq | awk '{print NR,$4}' >> trf2_5_band2eps_edit3.freq cat trf2_5_band2eps.freq | awk '{print NR,$5}' >> trf2_5_band2eps_edit3.freq cat trf2_5_band2eps.freq | awk '{print NR,$6}' >> trf2_5_band2eps_edit3.freq awk '{print 10,$1,$2}' trf2_5_band2eps_edit3.freq > trf2_5_band2eps_edit2.freq This one doesn't work. I would appreciate any help with it: #!/bin/sh cat trf2_5_band2eps.freq | awk '{print NR,$1}' > trf2_5_band2eps_edit3.freq cat trf2_5_band2eps.freq | awk '{for(i=2; i<=6; i++) print NR,$i}' >> trf2_5_band2eps_edit3.freq awk '{print 10,$1,$2}' trf2_5_band2eps_edit3.freq > trf2_5_band2eps_edit2.freq Thank you all again! Igor |
Quote:
So did you bother to try my solution? It is number of columns independent. Quote:
So your second line will print the line number (NR) 5 times along with the corresponding field ($i) from that line Hence why I have stored the results into an array to be delivered at the END of the script. I also find it curious why you use 'cat' to pass the contents of a file to a program that reads the contents of a file??? |
Quote:
Quote:
Hence why I have stored the results into an array to be delivered at the END of the script. Quote:
Igor |
Also, a new issue arrised in the meanwhile. My data are of the format 2.33456D-05 and so on. I the end, I need them multiplied by a constant factor (decimal number 219474.6306726). But when I do that, it seems that awk doesn't understand the format of my data, and gives back incorrect results.
I know that one can ask for a certain format with printf, but how can I tell awk that the input data are of that format, so that it can read them correctly? thnx Igor |
Quote:
Also, why not Perl? |
Awk works in double precision but it's not aware of the fortran D notation. You should first convert D to E. Following my previous suggestion (but it can be applied for all the others) you can try something like:
Code:
eval cat $(seq -f "<(awk '{sub(/D/,\"E\"); print $%.0f''*''219474.6306726}' file)" 1 6) | nl Code:
for i in $(seq 1 6) |
All times are GMT -5. The time now is 09:46 PM. |