GIDForums  

Go Back   GIDForums > Computer Programming Forums > Miscellaneous Programming Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Sep-2007, 15:09
harris2107 harris2107 is offline
New Member
 
Join Date: Aug 2007
Posts: 6
harris2107 is on a distinguished road

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 12-Sep-2007, 16:42
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: appending with AWK


Quote:
Originally Posted by harris2107
I have two files called INPUT1 and INPUT2 and i am trying to append the values of INPUT1 to INPUT2 using AWK.

Just read one file into an array, then append an array element to each line of the second file:

Here is file comb.awk:
Code:
# Awk script for combining two files line-by-line { if(NR==FNR) { file1Record[NR]=$0; } else { printf("%s,",file1Record[FNR]); print; } }

File INPUT1:
Code:
PROD DEV QA

File INPUT2:
Code:
DB1,PRIMARY,NY DB2,SECONDARY,CA DB3,TERTIARY,MN

Command and output:
Code:
$ awk -f comb.awk INPUT2 INPUT1 DB1,PRIMARY,NY,PROD DB2,SECONDARY,CA,DEV DB3,TERTIARY,MN,QA
Quote:
Originally Posted by harris2107
Any other way of achieving this without using AWK?
Why did you make the title "appending with AWK," and then ask for ways without AWK? There are probably about a million ways (maybe more).

Do you want it with AWK or not? Make up my mind!


If you have the GNU text utility paste installed (as, I think, most Linux distros do), then:

Code:
$paste -d, INPUT2 INPUT1 DB1,PRIMARY,NY,PROD DB2,SECONDARY,CA,DEV DB3,TERTIARY,MN,QA

Regards,

Dave
Last edited by davekw7x : 12-Sep-2007 at 17:36.
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Appending file baard C++ Forum 4 09-May-2007 06:23
Problem inserting (appending) in the middle of text file rajeev nair C++ Forum 3 17-Apr-2007 00:55
Repeated appending to strings anders C Programming Language 6 09-Mar-2007 13:55
txt appending DesperateCry C Programming Language 5 26-May-2005 05:18
[Tutorial] Standard I/O aaroncohn C Programming Language 20 27-Feb-2004 21:07

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:51.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.