merge columns from multiple files
Hi all
I want to merge columns (selectively) from several files and create a new file with the merge output. I saw some suggestions to use pr/paste to join the columns and then awk to pick-up the columns. Code:
pr -m -t -s\ file1 file2 | gawk '{print $4,$5,$6,$1}' Can someone suggest a automated way? Regards |
Depending on what you mean by column, colrm might be useful.
|
Hi
I can extract the required column from each file using awk in a for loop (or colrm). But how can I join the result of each of the for loop iteration columnwise and write to a file? Regards Vijay |
Well as usual an example would always help as we are shooting in the dark here, but how about just:
Code:
awk '{print $2}' file1 file2 file3 ... |
Got it! The code in your OP isn't an attempt to solve the problem, it's colucix's illustration of a general technique, copied from his post.
Do I now rightly understand that you want to copy the "second space-separated column"s from a set of hundreds of files into a the output file? Do all the files have the same number of lines? |
Quote:
Code:
awk '{print $2}' file1 file2 file3 ... I want the final output to be A1 B1 C1... A2 B2 C2... A3 B3 C3... If the original files had x A1 y z x A2 y z x A3 y z x B1 y z x B2 y z x B3 y z x C1 y z x C2 y z x C3 y z and so on... |
Quote:
But I may have to deal with variable number of lines at a later stage. Thanks. |
How many lines in the hundreds of files?
|
|
I am also considering writing rows instead of columns and then transposing the whole thing later. writing rows is easy :)
|
If it does not require too much memory (I cannot test with hundreds of files, right now), what about something like this?
Code:
awk '{_[FNR]=(_[FNR] OFS $2)}END{for (i=1; i<=FNR; i++) {sub(/^ /,"",_[i]); print _[i]}}' file* |
Hi colucix
This works fine, thanks. It is easy to understand also. Now when I try to apply this to files with unequal number of rows I am facing problem. If original files were: x A1 y z x A2 y z x B1 y z x B2 y z x B3 y z Output becomes A1 B1 A2 B2 B3 I would prefer A1 B1 A2 B2 [space]B3 I think this will be difficult as 'awk print' will not know how many blanks spaces to put before actually printing the variable. Anyway please let me know if you great guys have a solution for this also. Regards |
This is more complex. The basic idea is to count how many fields have been previously added for each row number, and while the count of fields does not match the count of files (minus one) add spaces accordingly.
However, in order to have a correctly formatted table, I assumed the maximum length of the string in the second field to be 8 characters and every blank space added will be 8 characters length (see the format in the sprintf statements). Here we go: Code:
BEGIN { Code:
$ awk -f test.awk file* |
Hey colucix, you inspired me to have a look at this one :)
Code:
{ |
Wonderful, grail! :) It is the more compact version I was looking for, but I lost myself in the if/else condition approach. Just a little add-on to your code, to remove the extra spaces at the beginning of each line:
Code:
{ Code:
sub(/^ /,"",_[x]) Well done, grail! You're awk skills have been improving a lot since I know you. +10 from me. |
All times are GMT -5. The time now is 06:02 AM. |