![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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. |
|
||||
|
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.. |
|
||||
|
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
Regards |
|
||||
|
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 |
![]() |
| Bookmarks |
| Tags |
| solaris |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|