appending with AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting appending with AWK
# 1  
Old 09-12-2007
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.
# 2  
Old 09-12-2007
look at 'man paste'
# 3  
Old 09-13-2007
hi

Hi,

I think this one can help you.
input:
a:
Code:
this,is
this,is
this,is

b:
Code:
first
second
third

code:
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[i],hou[i])
 }

 ' a b

output:

Code:
this,is,second
this,is,third
this,is,first

# 4  
Old 09-13-2007
Quote:
Originally Posted by summer_cherry
Hi,

I think this one can help you.
input:
a:
Code:
this,is
this,is
this,is

b:
Code:
first
second
third

code:
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[i],hou[i])
 }

 ' a b

output:

Code:
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?
# 5  
Old 09-18-2007
use var for awk

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

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

# 6  
Old 09-20-2007
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Appending different columns of multiple files in awk

Hello All, I have three input files cat file1 col1|col2|col3 a|1|A b|2|B cat file2 col1|col2|col3 c|3|C cat file3 col1|col2|col3 d|4|D e|5|E i want below output file4 col1|col2 a|1 (6 Replies)
Discussion started by: looney
6 Replies

2. Shell Programming and Scripting

awk appending values inside a for loop

Hi i have a 2 files say test1 and test2 with the following data. cat file test test1 i want to append the output from a awk one liner to both the files. for i in cat file;do awk '/ whats happening is its printing the output properly. but not appending the way it showing in print... (1 Reply)
Discussion started by: venkitesh
1 Replies

3. Shell Programming and Scripting

Awk, appending a number in the first column of a row with a condition

Hi everyone, I have a data file in which the data is stored in event blocks. What I would like to get is that the same file with every data row starting with the number of event block. So here is two event blocks from my file: <event> -2 -1 0 0 0 501 0.00000000000E+00 ... (2 Replies)
Discussion started by: hayreter
2 Replies

4. UNIX for Dummies Questions & Answers

Appending columns at the end of output using awk/sed

Hi , I have the below ouput, =====gopi===== assasassaa adsadsadsdsada asdsadsadasdsa sadasdsadsd =====kannan=== asdasdasd sadasddsaasd adasdd =====hbk=== asasasssa .... .. I want the output like as below, not able paste here correctly. (2 Replies)
Discussion started by: eeegopikannan
2 Replies

5. Shell Programming and Scripting

Awk/Sed - appending within file

hello all, First time post here. I have searched a bit but could not find an exact answer. I have about a week's experience of Sed and Awk, and am having fun, but am a little stuck. I am reformatting an xml file into json format. I have got this far: {"clients": ...and so on. What I want... (22 Replies)
Discussion started by: singerfc
22 Replies

6. Shell Programming and Scripting

appending zeros to numbers using awk

hi i want to append zeros to a given number ( varying digits). the total length of the output should be 10 digits. For example: 1)input is var=347 output should be NewVar=0000000347 2) input is var=123456 output should be NewVar=0000123456 i am able to acheive this using typeset... (1 Reply)
Discussion started by: somi2yoga
1 Replies

7. Shell Programming and Scripting

appending several columns with awk and paste

Hello, I am trying to solve for a couple of hours now the following problem: I have n files and would like to add the third column of each file to a new file: temp1.txt 1 2 3 1 2 3 1 2 3 temp2.txt 1 2 4 1 2 4 1 2 4 1 2 4 temp3.txt (2 Replies)
Discussion started by: creamcheese
2 Replies

8. UNIX for Dummies Questions & Answers

Appending to EOF using AWK

In a previous post I needed to walk through a list of files. This works fine, but when I attempt to use the nawk line (below) I can only write to first file that's found. The files I'm trying to write to (${target_dir}${product}.html) already exist and are created by another part of my script that... (1 Reply)
Discussion started by: ricksj
1 Replies

9. UNIX for Dummies Questions & Answers

SED or AWK: Appending a line Identifier that changes with paragraph?

Have another question that has been eluding me all day. I have data file I'm trying to reformat so that each line is appended with an ID code, but the ID code needs to update as it searches through the file. I.e. ----Begin Original Datafile----- Condition = XXX Header Line 1 Header... (1 Reply)
Discussion started by: selkirk
1 Replies

10. Shell Programming and Scripting

AWK scripting problem - appending values

ABC:10:A1:ABCA110 ABC:10:A1:ABCA110 ABC:20:A1:ABCA120 DEF:20:D1:DEFD120 GHI:30:G1:GHIG130 GHI:40:G1:GHIG140 JKL:30:J1:JKLJ130 MNO:10:M1:MNOM110 What I'm trying to do is look through a file that consists of four columns (as above). As you can see there are duplicates in the file, i.e.... (2 Replies)
Discussion started by: rowntree
2 Replies
Login or Register to Ask a Question