The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Append to end of each line of file without a temp file. rorey_breaker Shell Programming and Scripting 4 04-03-2008 07:11 AM
How to Append a Value to each line of the file dsshishya UNIX for Dummies Questions & Answers 11 03-21-2008 10:48 AM
Append line based on fixed position ashikin_8119 Shell Programming and Scripting 2 03-19-2008 12:09 AM
Append Line From a File To Another File panknil Shell Programming and Scripting 3 05-28-2007 10:15 PM
append field to file(nawk) axl Shell Programming and Scripting 4 11-14-2004 01:19 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-29-2005
Registered User
 

Join Date: Feb 2005
Posts: 4
Append a field to the end of each line of a file based on searching another file.

Hi All,

I have two comma separated value(CSV) files, say FileA and FileB.
The contents looks like that shown below.

FileA
EmpNo,Name,Age,Sex,
1000,ABC,23,M,
1001,DES,24,F,
1002,JHS,26,F,
1003,JOS,42,M,
...................


FileB
EmpNo,Spouse,
1000,DEB,
1002,FAR,
................


FileA has say 20000 lines and FileB has 1000. What I am trying to do is to append the spouse's name in the FileA if the employee is married. Else I need to append a N.A., or something. So for each EmpNo in FileA, I need to check FileB for a matching first column, and if a match is found append FileA with second column of FileB. Else Append a N.A. to the end of that line.

So my output file should be something like the one shown below.

FileC
EmpNo,Name,Age,Sex,Spouse,
1000,ABC,23,M,DEB,
1001,DES,24,F,N.A.,
1002,JHS,26,F,FAR,
1003,JOS,42,M,N.A.,

I know this can be done using awk or sed. But I am not much familier with them either. Please help me with some pointers to tackle this.. Some sample codes are most welcome..

Thanks in Advance.
Ultimate.

Last edited by ultimate; 03-29-2005 at 03:58 AM. Reason: Subject modification
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-29-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,999
nawk -f ult.awk FileB FileA

here's ult.awk:
Code:
BEGIN { FS=OFS="," }
FNR == NR { arr[$1]=$2; next }
{ $NF=($1 in arr) ? arr[$1] : "N.A."; print }
Reply With Quote
  #3 (permalink)  
Old 03-29-2005
Registered User
 

Join Date: Feb 2005
Posts: 4
Thanks a lot vgersh99!!
This exactly satisfies the need.. Now i realize that i know nothing about awk..

I have been trying for half a day to accomplish this.. Great help..

Btw, I did not have nawk, and tried with awk and it works fine.. Is there any big difference b/w both?

Last edited by ultimate; 03-29-2005 at 10:49 AM.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:49 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0