shell script for matching 2 files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script for matching 2 files
# 1  
Old 06-19-2008
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  
Old 06-19-2008
Does it have to be shell script or you can use Perl?
# 3  
Old 06-20-2008
Yes, shell script please.
Thanks.
# 4  
Old 06-20-2008
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  
Old 06-20-2008
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 03:39 PM..
# 6  
Old 06-20-2008
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  
Old 06-23-2008
Hi Sir Franklin,

Do you mind giving a brief explanation of the above code pls? =)
So i can use it in the future as well.

Thanks so much.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pattern matching and replace in shell script

Hi I want to find a line in a file which contains a word and replace the patterns. Sample file content temp.xml ==================== <applications> <application> Name="FirstService" location="http://my.website.selected/myfirstService/V1.0/myfirst.war" ... (1 Reply)
Discussion started by: sakthi.99it
1 Replies

2. Shell Programming and Scripting

Shell script matching similar records

hello all, I have requirement to identify similar records matching about 80% to 90%.I have to black list customers with multiple accounts. The data is in the Oracle Database, but is there any way I can get the data into flat file and compare the strings and fetch similar matching records? ... (2 Replies)
Discussion started by: kashik786
2 Replies

3. Shell Programming and Scripting

Help me to find files in a shell script with any matching pattern

Hi friends.. I have many dirs in my working directory. Every dir have thousands of files (.jsp, .java, .xml..., etc). So I am working with an script to find every file recursively within those directories and subdirectories ending with .jsp or .java which contains inside of it, the the pattern... (3 Replies)
Discussion started by: hnux
3 Replies

4. Shell Programming and Scripting

Removing matching text from multiple files with a shell script

Hello all, I am in need of assistance in creating a script that will remove a specified block of text from multiple .htaccess files. (roughly 1000 files) I am attempting to help with a project to clean up a linux server that has a series of unwanted url rewrites in place, as well as some... (4 Replies)
Discussion started by: boxx
4 Replies

5. Homework & Coursework Questions

shell script that can create, monitor the log files and report the issues for matching pattern

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write an automated shell program(s) that can create, monitor the log files and report the issues for matching... (0 Replies)
Discussion started by: itian2010
0 Replies

6. Shell Programming and Scripting

Pattern matching in shell script

Hi, I am using following command to extract string from a file. String will be after last / (slash). awk -F\ / '{print $NF}' $FILE but while appending the output in file in script, it dosent work. File created but of zero size... can anyone please help `awk -F\\\/ '{print $NF}' $FILE` >... (3 Replies)
Discussion started by: Deei
3 Replies

7. Shell Programming and Scripting

shell script pattern matching

Hi, I need to create a shell script through which i need to populate email addresses in email columns of database table in mysql. Let say if email contains yahoo, hotmail, gtalk than email addresses need to move in their respective columns. # !/bin/sh yim="example@yahoo.com"... (3 Replies)
Discussion started by: mirfan
3 Replies

8. Shell Programming and Scripting

Pattern matching in a shell script?

I'm looking for a way to match a particular string in another string and if a match is found execute some command. I found the case statement can be used like this; case word in ) command ;; ] ... esac If my string to find is say "foo" in the string $mystring... (1 Reply)
Discussion started by: paulobrad
1 Replies

9. UNIX for Dummies Questions & Answers

matching 3 patterns in shell script

IN a file I need to check for 3 patterns if all the 3 patterns are in the file. I need to send out an email. All this needs to be done in korn shell script. Please advise. (1 Reply)
Discussion started by: saibsk
1 Replies

10. Shell Programming and Scripting

C shell script for time matching

Write a C shell script called greetings which will print out one of Good morning, Good afternoon or Good evening depending on the time of day. . (1 Reply)
Discussion started by: Ringo
1 Replies
Login or Register to Ask a Question