The UNIX and Linux Forums  

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
match field between 2 files phamp008 Shell Programming and Scripting 8 01-23-2008 07:46 AM
sed - Replace Line which contains the Pattern match with a new line kousikan Shell Programming and Scripting 2 03-24-2007 07:24 AM
sort & match multiple files nazri76 Shell Programming and Scripting 4 07-28-2006 03:17 AM
Match & append the files shashi_kiran_v UNIX for Dummies Questions & Answers 2 12-14-2005 07:21 AM
Replace text in match files wilsonchan1000 UNIX for Dummies Questions & Answers 1 12-16-2002 09:21 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-19-2008
srsahu75 srsahu75 is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 15
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 (permalink)  
Old 06-19-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,348

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

Regards
  #3 (permalink)  
Old 06-21-2008
srsahu75 srsahu75 is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 15
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 (permalink)  
Old 06-21-2008
srsahu75 srsahu75 is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 15
Thanks

Quote:
Originally Posted by Franklin52 View Post
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
Closed Thread

Bookmarks

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 07:10 AM.


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