[Solved] Lookup a file and match the contents


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Lookup a file and match the contents
# 1  
Old 02-08-2012
[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.
# 2  
Old 02-08-2012
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
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 02-08-2012
Thank you.
Its working.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk base lookup of best match strings

Hi, I'm new to scripting and unable to find out a way to perform the below task. Request help in finding out a way to accomplish this. File one consists of some numbers/string which i need to lookup against file 2 and fetch the best match results in output. If best match is not present in... (3 Replies)
Discussion started by: suraj016
3 Replies

2. Shell Programming and Scripting

awk match to update contents of file

I am trying to match $1 in file1 with $2 in file2. If a match is found then $3 and $4 of file2 are copied to file1. Both files are tab-delimeted and I am getting a syntax error and would also like to update file1 in-place without creating a new file, but am not sure how. Thank you :). file1 ... (19 Replies)
Discussion started by: cmccabe
19 Replies

3. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

4. Shell Programming and Scripting

Move parent folder if contents match a parameter

Hi all, Hoping someone can help. I'm looking to be able to search a particular folder path for folders that contain certain files / formats and then move the parent folder. eg. /foo/bar/folder1/file.exe /foo/bar/folder2/file.exe and then move the folder1/2 tp another folder. So if... (1 Reply)
Discussion started by: springs2
1 Replies

5. UNIX for Dummies Questions & Answers

Help with AWK - Compare a field in a file to lookup file and substitute if only a match

I have the below 2 files: 1) Third field from file1.txt should be compared to the first field of lookup.txt. 2) If match found then third field, file1.txt should be substituted with the second field from lookup.txt. 3)Else just print the line from file1.txt. File1.txt:... (4 Replies)
Discussion started by: venalla_shine
4 Replies

6. Shell Programming and Scripting

match contents of file with log file

Hello folks, I have 10 lines of file i want to check that any line from 1-to-10 is available in access.log or not, if yes print that complete line. (2 Replies)
Discussion started by: learnbash
2 Replies

7. UNIX for Dummies Questions & Answers

[Solved] Symbolic links - viewing contents

Hi, I did do a search and there are lots of threads about creating/deleting symbolic links but nothing about actually viewing the target of a link. I have the following: bash> ls -l /usr/bin/python lrwxrwxrwx 1 root other 9 Apr 22 16:08 /usr/bin/python -> python2.6 Fine - but now I... (2 Replies)
Discussion started by: nelmo
2 Replies

8. UNIX for Dummies Questions & Answers

AWK lookup not finding match

Hello everyone, I have been struggling with the following situation, I think I am doing something wrong, can anyone help? I have 2 comma separated files, the first is a look-up table that will supply the phone number based on the customer id, the second is a file containing customers and their... (4 Replies)
Discussion started by: gio001
4 Replies

9. HP-UX

[Solved] How to view the contents of the .depot files

Hi, I have one depot file. I would want to view the contents of this file with out extracting and with out installing in a machine. Like for $rpm -qlp rpmfilename will list out all the files in a rpm. Like I would want a command to view the files from a .depot file. I tried with swlist... (2 Replies)
Discussion started by: skmdu
2 Replies

10. UNIX for Advanced & Expert Users

Clueless about how to lookup and reverse lookup IP addresses under a file!!.pls help

Write a quick shell snippet to find all of the IPV4 IP addresses in any and all of the files under /var/lib/output/*, ignoring whatever else may be in those files. Perform a reverse lookup on each, and format the output neatly, like "IP=192.168.0.1, ... (0 Replies)
Discussion started by: choco4202002
0 Replies
Login or Register to Ask a Question