Osama Ahmed
Osama Ahmed

Reputation: 69

How to combine files

I have two files.

first file:

45  76
77  23
12  93
77  10
82  92

second file:

89
37
84
10
93

I want to combines them in one file like this

89  45  76
37  77  23
84  12  93
10  77  10
93  82  92

Upvotes: 1

Views: 88

Answers (3)

iruvar
iruvar

Reputation: 23384

paste is the canonical tool to solve this. Here is a pure bash alternative

while IFS= read -r -u 3 line1 && IFS= read -r -u 4 line2;
do
  printf "%s %s\n" "$line2" "$line1";
done 3<first 4<second

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172588

You can try this:-

$ join file1.txt file2.txt

If the files are not sorted try like this:

$ paste file2.txt file1.txt

Upvotes: 0

Chris Seymour
Chris Seymour

Reputation: 85873

With paste:

$ paste file2 file1
89  45  76
37  77  23
84  12  93
10  77  10
93  82  92

With pr:

$ pr -mts'  ' file2 file1
89  45  76
37  77  23
84  12  93
10  77  10
93  82  92

With awk:

$ awk 'NR==FNR{a[NR]=$0;next}{print a[FNR],$0}' OFS='  ' file2 file1
89  45  76
37  77  23
84  12  93
10  77  10
93  82  92

Upvotes: 3

Related Questions