Merging two files in awk

Hi,

How I can merge two file columns such as the followings using awk:
file 1
2 3
2 2
1 1
file 2
4 3
4 5
7 6

Result:
2 3 4 3
2 2 4 5
1 1 7 6

This is an example, at the end, I will have about 25 files that I want to merge them, it is important for me that the order in the columns merged will be the order of the files.

Sorry for my simple questions but I am so new in programming and awk but I have to do a lot.

Thank you so much.

You can use paste command to achieve this:-

paste file1 file2
1 Like

My files will be so huge, of about 50 fields and 400,000 rows, using paste, won't it take too much of the memory?

I'm not sure about the memory utilization. If you insist a solution using awk here it is:-

awk 'NR==FNR{a[FNR]=$0;next} {print a[FNR], $0 } ' file1 file2
1 Like

that works perfect, thank you so much.

Alternative awk:

awk '{getline $(NF+1)<f}1' f=file2 file1
1 Like

It works well with 2 files but when I add a third file with the same number of lines but different number of columns, that is for example:
snp123
snp456
snp789

it doesn't work. Also, when I want to add three files like each other for example:
1 2
1 2
2 3

it also doesn't give me the right file format.

What I want to do finally is merging one file with one column and 25 files each with 2 columns into each other, at the end I will have a file with 26 columns and the number of rows of all files are equal.

Thank you so much for your help.

Just use paste as first suggested by bipinajith. It was created for this specific task.

Memory consumption shouldn't be an issue since paste doesn't need to read more than one line at a time to do its job -- unlike bipinajith's awk approach which stores an entire file in memory.

While scrutinizer's approach isn't memory hungry, it isn't as easily extended to 3 or more files as a simple paste command.

On an unrelated note, use code tags to preserve the formatting of sample data and code. Also, when reporting that something doesn't work, show the exact command that you are running, any error messages emitted by the command, and a minimized set of input data which triggers the error. Further, always tell us which operating system and shell you're working with.

Regards,
Alister

1 Like