Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write a two column file from two files using awk

I have two files of one column each

1
2
3

and

4
5
6

I want to write a unique file with both elements as

1 4
2 5
3 6

It should be really simple I think with awk.

like image 996
Valerio D. Ciotti Avatar asked Dec 16 '22 13:12

Valerio D. Ciotti


2 Answers

You could try paste -d ' ' <file1> <file2>. (Without -d ' ' the delimiter would be tab.)

like image 55
FooF Avatar answered Jan 01 '23 14:01

FooF


paste works okay for the example given but it doesn't handle variable length lines very well. A nice little-know core-util pr provides a more flexible solution:

$ pr -mtw 4 file1 file2
1 4
2 5
3 6 

A variable length example:

$ pr -mtw 22 file1 file2
10         4
200        5
300,000,00 6

And since you asked about awk here is one way:

$ awk '{a[FNR]=a[FNR]$0" "}END{for(i=1;i<=length(a);i++)print a[i]}' file1 file2
1 4 
2 5 
3 6
like image 45
Chris Seymour Avatar answered Jan 01 '23 16:01

Chris Seymour