[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.

Try this...

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

1 Like

Thank you.
Its working.