The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 10:11 AM
How to Append a Value to each line of the file dsshishya UNIX for Dummies Questions & Answers 11 03-21-2008 02:48 PM
Append line based on fixed position ashikin_8119 Shell Programming and Scripting 2 03-19-2008 04:09 AM
Append Line From a File To Another File panknil Shell Programming and Scripting 3 05-29-2007 01:15 AM
append field to file(nawk) axl Shell Programming and Scripting 4 11-14-2004 05:19 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-29-2005
ultimate ultimate is offline
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 06:58 AM.. Reason: Subject modification
  #2 (permalink)  
Old 03-29-2005
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
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 }
  #3 (permalink)  
Old 03-29-2005
ultimate ultimate is offline
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 01:49 PM..
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes 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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:22 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0