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
Matching and combining two files p3t3r Shell Programming and Scripting 5 05-20-2008 10:16 AM
matching 3 patterns in shell script saibsk UNIX for Dummies Questions & Answers 1 01-11-2008 03:06 PM
Find matching lines between 2 files jojojmac5 UNIX for Dummies Questions & Answers 5 01-18-2007 01:06 PM
Grep all files matching partial filename mharley Shell Programming and Scripting 3 06-08-2005 02:17 PM
C shell script for time matching Ringo Shell Programming and Scripting 1 08-07-2003 01:26 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 06-19-2008
gholdbhurg gholdbhurg is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 26
shell script for matching 2 files

Hi,
Would anyone be able to suggest on this shell script query? =)
From the contents of the 2 sample files below, if both files' 1st fields matched (ex: XYZ1234=XYZ1234), I want to append the 4th field (semi-colon delimited) from File2 to the end of that line in File1.
If no match found, then simply append 0000.00 (zero)

File1:
XYZ1234 |Golden State | 500000.00| 10-JUNE-2008| 0654321|Model Railroad Museum
ABC5678 |Philadelphia | 350000.50| 10-JUNE-2008| 0135790|Avenue of Arts
LMN3579 |Seattle | 1180000.00| 15-JUNE-2008| 0427508|Elysian Brewing
RQP8642 |Charlotte | 98500.50| 17-JUNE-2008| 0315386|Paramount's Carowind
TYU4938 |Milwaukee | 85000.50| 17-JUNE-2008| 0526874|Betty Brinn Museum

File2:
XYZ1234;250;TYREQW; 150000.00;10-JUNE-2008;5463789KJH
RQP8642;999;FGHDSA; 55000.50;17-JUNE-2008;4637826TGF
LMN3579;345;CVXZBN; 1000000.00;15-JUNE-2008;8763943NBD
ABC5678;075;PYTIRY; 105000.50;10-JUNE-2008;3792541XOR
XKW4739;123;EKFZHI; 95000.50;15-JUNE-2008;1468953WLC

Required output (new File1):
XYZ1234 |Golden State | 500000.00| 10-JUNE-2008| 0654321|Model Railroad Museum | 150000.00
ABC5678 |Philadelphia | 350000.50| 10-JUNE-2008| 0135790|Avenue of Arts | 105000.50
LMN3579 |Seattle | 1180000.00| 15-JUNE-2008| 0427508|Elysian Brewing | 1000000.00
RQP8642 |Charlotte | 98500.50| 17-JUNE-2008| 0315386|Paramount's Carowind | 55000.50
TYU4938 |Milwaukee | 85000.50| 17-JUNE-2008| 0526874|Betty Brinn Museum | 0000.00

Thanks much in advance.
  #2 (permalink)  
Old 06-19-2008
odashe odashe is offline
Registered User
  
 

Join Date: May 2005
Posts: 31
Does it have to be shell script or you can use Perl?
  #3 (permalink)  
Old 06-20-2008
gholdbhurg gholdbhurg is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 26
Yes, shell script please.
Thanks.
  #4 (permalink)  
Old 06-20-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
What have you done to attempt to solve this problem yourself?
Post your sample script, and we'll see how we can assist.

Regards
  #5 (permalink)  
Old 06-20-2008
gholdbhurg gholdbhurg is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 26
Hi Sir Franklin,
Sincerely appreciate your response.
I actually have a little idea of how i want to go about this, but i just seem to have a hard time putting everything exactly in code.

I want to loop the File1 line by line (storing the 1st fields, i.e XYZ1234) then use this variable to search in File2, and then get the 5th field from that same equation.
Ex: grep XYZ1234 File2 | awk -F";" '{print $4}'
Then will append that output to the end of the 1st line in File1.
Same will be done for the rest of the lines in File1.

Thanks so much in advance.

Last edited by gholdbhurg; 06-20-2008 at 02:39 PM..
  #6 (permalink)  
Old 06-20-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
This should gives the desired output if the format of your files are exact as the given samples:

Code:
awk '
NR==FNR{FS=";";$0=$0;a[$1]=$4;next}
{FS=" |";$0=$0}
a[$1]{print $0" |"a[$1];next}
{print $0"| 0000.00"}
' File2  File1
If you get errors use nawk, gawk or /usr/xpg4/bin/awk on Solaris.

Regards
  #7 (permalink)  
Old 06-23-2008
raghav288 raghav288 is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 21
Hi Gholdbhurg

this code may help you:

i=1
while true
do
#Get the line from File1
line=`sed -n ${i}p File1`

#Get the first field in the line
code=`echo $line | cut -d'|' -f1`

#EOF
if [ -z "$code" ]
then
exit 0;
fi

#Get the 4th field of the File2
data=`sed -n '/'$code'/p' File2 | cut -d ';' -f4

#Join the data
output="${line}|${data}"

#append it to the file
echo $output>>newFile1

i=`expr $i + 1`

done
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 02:00 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