Hanfei Sun
Hanfei Sun

Reputation: 47051

Merge two files (use line number instead of common field)

Input file1:

1
5
6

Input file2:

4.5
6.7
9.9

Output:

1  4.5
5  6.7
6  9.9

I tried join but it doesn't work here as there's no common field. Does anyone have ideas about this?

Upvotes: 2

Views: 1250

Answers (2)

Vijay
Vijay

Reputation: 67211

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

tested below:

> cat file2
a 
b
c
> cat file1
1 
100
90
> awk 'FNR==NR{a[NR]=$1;next}{print a[FNR],$0}' file2 file1
a 1 
b 100
c 90

Upvotes: 2

paxdiablo
paxdiablo

Reputation: 881253

I think you're looking for the paste command rather than join, as per the following transcript:

pax> cat file1
1
5
6

pax> cat file2
4.5
6.7
9.9

pax> paste file1 file2
1   4.5
5   6.7
6   9.9

Consult your friendly neighbourhood man page (or info page) for more details, such as using -d to select delimiters other than TAB, or -s to process the files sequentially rather than in parallel.

pax> paste -d= file1 file2
1=4.5
5=6.7
6=9.9

pax> paste -d, -s file1 file2
1,5,6
4.5,6.7,9.9

Upvotes: 11

Related Questions