LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   awk command to merge columns from two separate files into single file? (http://www.linuxquestions.org/questions/linux-newbie-8/awk-command-to-merge-columns-from-two-separate-files-into-single-file-522016/)

johnpaulodonnell 01-23-2007 09:27 AM

awk command to merge columns from two separate files into single file?
 
Hi.

Does anyone know an awk command that will take the first column from file1 say, and add it as a column to file2? Everywhere I read seems only to use single file examples...

Thanks.

colucix 01-23-2007 09:56 AM

Awk scripting let do (almost) every thing. However, if the two files contain the same number of rows, you can merge them together and then extract the column you want with a single call to awk:

Code:

> cat file1
one two three
one two three
one two three
one two three

> cat file2
four five six
four five six
four five six
four five six

> pr -m -t -s\  file1 file2 | gawk '{print $4,$5,$6,$1}'
four five six one
four five six one
four five six one
four five six one

See man pr for details. Note the double space in the pr command: the option "-s\ " tells to merge the two files using a single space as separator. Hope this will help.

pwc101 01-23-2007 10:20 AM

If I remember correctly, the paste command will stick the two files together (all columns), from which you can then awk the desired columns.

johnpaulodonnell 01-23-2007 10:40 AM

Thanks for that.

druuna 01-23-2007 11:10 AM

Hi,

Although I like colucix solution better, here's a awk script that can handle 2 infiles (answered this a while back):

awk with 2 input files


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