0

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?

Chris Seymour
  • 79,902
  • 29
  • 153
  • 193
Rohit
  • 8,548
  • 23
  • 63
  • 103
  • Look at this question, it may have your answer. http://stackoverflow.com/questions/5467690/how-to-merge-two-files-using-awk – Ibrar Ahmed Mar 16 '13 at 21:18

2 Answers2

4

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
Chris Seymour
  • 79,902
  • 29
  • 153
  • 193
2

Use the slightly overlooked tool pr:

$ pr -m -t -s,  file1 file2
a,0
b,1
c,2
Fredrik Pihl
  • 42,950
  • 7
  • 81
  • 128