Sahil
Sahil

Reputation: 9480

Append output of two files in shell?

I have two files like this

file1

a
b
c

file2

0
1
2

I want to output

a,0
b,1
c,2

Appending the two files like this

row(n) of file1 +","+ row(n) file2
for every n, total n is same in both files

I want to know is there any utility in shell which can help me do this, I do not want to use java file read file write for this or any loops. Can it be done using awk?

Upvotes: 0

Views: 178

Answers (2)

Fredrik Pihl
Fredrik Pihl

Reputation: 45644

Use the slightly overlooked tool pr:

$ pr -m -t -s,  file1 file2
a,0
b,1
c,2

Upvotes: 2

Chris Seymour
Chris Seymour

Reputation: 85775

You want paste:

$ paste -d',' file1 file2
a,0
b,1
c,2

It can be done many ways in awk here is one:

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

Upvotes: 4

Related Questions