appending with AWK

Hi all,
I have two files called INPUT1 and INPUT2 and i am trying to append the values of INPUT1 to INPUT2 using AWK.

The INPUT1 file will always has a single field and the INPUT2 file has 3 fields but both files will have same number of records always. Following is the format of the two files,

INPUT1:

PROD
DEV
QA

INPUT2:

DB1,PRIMARY,NY
DB2,SECONDARY,CA
DB3,TERTIARY,MN

Now, the issue i am working on is to append the values of INPUT1 to the file INPUT2 using awk. So, after appending the output should look like following,

DB1,PRIMARY,NY,PROD
DB2,SECONDARY,CA,DEV
DB3,TERTIARY,MN,QA

i.e., the first record of INPUT1 should be appended to the 1st record (last field) of INPUT2 and so on...... As i said the number of records in each file will be same. So, each record in file1 should be appended to corresponding record in file2.

Any other way of achieving this without using AWK?

Any suggestions?

Thanks,
Harris.

look at 'man paste'

Hi,

I think this one can help you.
input:
a:

this,is
this,is
this,is

b:

first
second
third

code:

awk 'BEGIN{FS=","}
 {
 if (NF==1)
 hou[NR]=$0
 if (NF>1)
 qian[NR-3]=$0
 }
 END{
 for (i in hou)
 printf("%s,%s\n",qian,hou)
 }

 ' a b

output:

this,is,second
this,is,third
this,is,first

what what happens if you don't have exactly THREE records in either file 'a' or 'b'? Say, you have 5 records in each?

For above case, we can use a variable and pass it to awk.

nawk -v a=10 'BEGIN{print a}'

Very intuitive program. Hats off.

But there are 2 errors in this code:

1) we should swap the inputs, we should pass the 'b' file first and then 'a', since the first if statement checks for NF==1, as 'b' file is having single field. Otherwise the if statements in the code should be swapped. As per your order of input with this code the output will be like:
,first
,second
,third

2) This code will not work if the file 'b' contains more than 1 field. If that case exists, what to do? Because the output would become,
this, is,
this, is,
this, is,
first, 1
second, 2
third, 3
(where 1, 2, 3 field I added in the file 'b')

You already given the solution for the problem, if the file contains more than 3 records by assigning a variable. So could you also explain us how to make this awk script more generic (i,e) it should work independent of the no. of fields on both the files, keeping the no of lines (records) intact?

Thanks