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
Combining two files hemangjani Shell Programming and Scripting 7 06-13-2007 10:32 PM
Combining Two Files stevefox Shell Programming and Scripting 4 02-20-2006 05:09 AM
Combining Two Files bat711 Shell Programming and Scripting 3 10-05-2005 01:26 PM
Combining files Enda Martin UNIX for Dummies Questions & Answers 2 07-20-2001 10:31 AM
combining files apalex UNIX for Dummies Questions & Answers 3 06-19-2001 09:49 AM

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 05-20-2008
p3t3r p3t3r is offline
Registered User
  
 

Join Date: May 2008
Posts: 3
Matching and combining two files

Hi, How can I match the first two fields of file2 against the first two fields of file1 and where they match combine the two lines. If the name
(example-Aidan Rielly) is in file1 but not in file2 then just write the info from file1 to the combined output file. If the name
(example-Silvia Spoelstra) is in file2, but not in file1, then do not write it to the combined output file.

file1
Firstname,Surename,City
Aidan,Rielly,Derry
John,Doe,London
Jack,Daniles,Glasgow
Ivor,Engine,Cardif

file2
Firstname,Surname,Country,ISO
Jack,Daniels,Scotland,GB
John,Doe,England,UK
Ivor,Engine,Wales,UK
Silvia,Spoelstra,Germany,DE

Combined file
Firstname,Surename,City,Country,ISO
Aidan,Rielly,Derry
John,Doe,London,England,UK
Jack,Daniles,Glasgow,Scotland,GB
Ivor,Engine,Cardif,Wales,UK

thanks in advance
Pete

Last edited by p3t3r; 05-20-2008 at 07:04 AM..
  #2 (permalink)  
Old 05-20-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
This should gives the desired output:

Code:
awk -F, '
BEGIN{print "Firstname,Surename,City,Country,ISO"}
NR==FNR{a[$1$2]=$3","$4;next}
a[$1$2]{print $0 ","a[$1$2];next}1
' file2 file1
Regards
  #3 (permalink)  
Old 05-20-2008
p3t3r p3t3r is offline
Registered User
  
 

Join Date: May 2008
Posts: 3
Hi, thanks for the quick response. I get a syntax error when I try this.

$ awk -F, '
> BEGIN{print "Firstname,Surename,City,Country,ISO"}
> NR==FNR{a[$1$2]=$3","$4;next}
> a[$1$2]{print $0 ","a[$1$2];next}1
> ' file2 file1
awk: syntax error near line 4
awk: bailing out near line 4

any idea ?
Pete
  #4 (permalink)  
Old 05-20-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
A common problem is that there are different versions of awk. You probably have the original "One True" awk; see if you can find nawk or mawk or gawk on your system. Also on Sun and HP-UX look for the XPG4 version.
  #5 (permalink)  
Old 05-20-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
As era mentioned, if you get errrors use nawk or /usr/xpg4/bin/awk on Solaris.

Regards
  #6 (permalink)  
Old 05-20-2008
p3t3r p3t3r is offline
Registered User
  
 

Join Date: May 2008
Posts: 3
works perfectly with nawk.

Thankyou very much
Pete
Closed Thread

Bookmarks

Tags
solaris

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:48 PM.


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