Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-08-2012
Registered User
 

Join Date: Feb 2011
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
[Solved] Lookup a file and match the contents

Hi,

I appreciate all who have been very helpful to me in providing valuable suggestions and replies.

I want to write a script to look up a file and match the contents. Let me go through the scenario. Lets say i have two files

Content file:
abc, bcd, adh|bcdf|adh|wed
bcf, cdf, wed|erd|dfg|wed

Lookup File:
adh-red
wed-blue

3rd column of content file has many words separated by pipe. I have to leave f1st and 2nd word of 3rd column as is, and replace the 3rd and 4th words with Lookup file contents. Only first 2 words needs to be retained and the rest ( can be more than 4 ) needs to replaced with lookup file contents.

Output file should be like this:
abc, bcd, adh|bcdf|red|blue
bcf, cdf, wed|erd|dfg|blue

if you see the output file, only last 2 words ( bold ) of 3rd column are modfied.
Can i know how can i do this using Unix/Linux scripts.

I wish to modify the contents of content file and write back to the same file without hurting rest of the columns.

I appreciate your responses.
Sponsored Links
    #2  
Old 02-08-2012
ahamed101's Avatar
root is god!!!
 

Join Date: Sep 2008
Location: India
Posts: 1,478
Thanks: 33
Thanked 382 Times in 377 Posts
Try this...

Code:
awk -F"[|-]" 'NR==FNR{a[$1]=$2;next}{for(i=3;i<=NF;i++){if($i in a)sub($i,a[$i],$i)}print}' OFS="|" lookupfile contentfile

--ahamed
Sponsored Links
    #3  
Old 02-08-2012
Registered User
 

Join Date: Feb 2011
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you.
Its working.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[Solved] Replacing a particular string in all files in folder and file contents Candid247 UNIX for Dummies Questions & Answers 0 01-04-2012 12:02 AM
AWK lookup not finding match gio001 UNIX for Dummies Questions & Answers 4 10-27-2010 08:49 PM
[Solved] How to view the contents of the .depot files skmdu HP-UX 2 03-30-2010 04:34 AM
How to match all array contents and display all highest matched sentences in perl? vanitham Shell Programming and Scripting 5 06-18-2009 02:02 AM
Clueless about how to lookup and reverse lookup IP addresses under a file!!.pls help choco4202002 UNIX for Advanced & Expert Users 0 08-26-2008 01:28 PM



All times are GMT -4. The time now is 04:43 AM.