Match and replace in different files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match and replace in different files
# 1  
Old 06-19-2008
Match and replace in different files

Hi,
I want to replace the content in 3rd column in the first file by the line in the 2nd file (a list) if matches with 1st column of 2nd file. There are many 1st files in different directories:

File1

172.27.1.15 222 I_J_Mar_Sci_34_27.pdf
172.27.28.1 489 Sci_Cult_71_60.pdf
172.27.12.5 98 chap7_ref.pdf
172.27.1.73 98 chap7_ref.pdf

File2

chap7_ref.pdf NAP
chap1.pdf NAP
I_J_Mar_Sci_34_27.pdf NACP
Sci_Cult_71_60.pdf OAP
Human.pdf NAP

Required Output

172.27.1.15 222 I_J_Mar_Sci_34_27.pdf NACP
172.27.28.1 489 Sci_Cult_71_60.pdf OAP
172.27.12.5 98 chap7_ref.pdf NAP
172.27.1.73 98 chap7_ref.pdf NAP

Is it possible to solve the problem with shell script or Perl script and what will be the code?
# 2  
Old 06-19-2008
Code:
awk '
NR==FNR{a[$1]=$2;next}
a[$3]{$0=$0" "a[$3]}1
' file2 file1

Regards
# 3  
Old 06-21-2008
Thanks for your reply.
I have little doubt.....If in the File1 the 3rd coumn and 2nd column interchanged then where I need to change in code.
# 4  
Old 06-21-2008
Thanks

Quote:
Originally Posted by Franklin52
Code:
awk '
NR==FNR{a[$1]=$2;next}
a[$3]{$0=$0" "a[$3]}1
' file2 file1

Regards
Thanks for your code. I could able to my required task. I also could able to do the task in files where the columns inter change
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Data match 2 files based on first 2 columns matching only and join if match

Hi, i have 2 files , the data i need to match is in masterfile and i need to pull out column 3 from master if column 1 and 2 match and output entire row to new file I have tried with join and awk and i keep getting blank outputs or same file is there an easier way than what i am... (4 Replies)
Discussion started by: axis88
4 Replies

2. Shell Programming and Scripting

Match value in two files and replace values in selected columns

The purpose is to check if values for column 3 and 4 in file1 match with column 1 in file2. If any value match do: 1) Replace values in file2 for column 2 and 3 using the information of file1 columns 5 and 6 2) Replace string ($1,1,5) and string ($1,6,5) in file2 with values of columns 7... (8 Replies)
Discussion started by: jiam912
8 Replies

3. Shell Programming and Scripting

2 files replace multiple occurances based on a match

Hi All, I need some help trying to achieve the below but everything I've tried has failed, I have 2 files which i'm trying to carry out a match based on the first column from file 1, take that value find it in file 2 if found replace it with the second column from File 1 Lookup File: File 1... (3 Replies)
Discussion started by: mutley2202
3 Replies

4. Shell Programming and Scripting

Partial Match and Replace

Hi, I have a tab delimited text file like this one. I need to do a partial match of a particular cell and then replace matches with an empty cell. So here is a sample: Smith FordMustang ChevroletCamaro Miller FordFiesta Jones KiaSorrento Davis ChevroletCamaro Johnson ToyotaHighlander I... (4 Replies)
Discussion started by: mikey11415
4 Replies

5. Shell Programming and Scripting

awk to match field between two files and use conditions on match

I am trying to look for $2 of file1 (skipping the header) in $2 of file2 (skipping the header) and if they match and the value in $10 is > 30 and $11 is > 49, then print the line from file1 to a output file. If no match is foung the line is not printed. Both the input and output are tab-delimited.... (3 Replies)
Discussion started by: cmccabe
3 Replies

6. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

7. Shell Programming and Scripting

Match and replace

Hi Friends, This might sound a pure biological questions, but all help is highly appreciated. I have a file with the following style file1 >hg19_chr1_143_456_- aaaaaaaaaaaaabbbbbbbbbbbcccccccccccccccddddddddddddddddeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffffff... (7 Replies)
Discussion started by: jacobs.smith
7 Replies

8. Shell Programming and Scripting

Help with Pattern match and replace

I have a file containing a multiple lines of the format sddfdsf_gaf/ywrtrtwrt_gaf ghfghfgh_ert/xcvxcvcv_ert werwerwwerw_adf/jkhjkhjkjhkjhk_adf I am interested in only the first 3 letters following the "_" character and make those 3 letters uppercase after extraction. So would like to convert... (5 Replies)
Discussion started by: inditopgun
5 Replies

9. Shell Programming and Scripting

Match pattern and replace

Hi All, I am new to unix shell scripting, I need your help guys in coming up with some thing for the following scenario: file1 ABC_BASE ${base} ABC_ACC ${acc} ABC_TEST ${test} 01-01-2006 ${from_dt} 01-15-2006 ${to_dt} file 2 I have an file2.sql file which contains: ####This... (4 Replies)
Discussion started by: sol_nov
4 Replies

10. UNIX for Dummies Questions & Answers

Replace text in match files

Hi all, I want to replace text 'DEF' for those files containing text 'ABC'. I can only locate the files containing text 'ABC', but I don't know how to replace the text 'ABC' with 'DEF'. I use the following command to locate the files containing 'ABC' find . -exec grep -l 'ABC'... (1 Reply)
Discussion started by: wilsonchan1000
1 Replies
Login or Register to Ask a Question