|
Search Forums:
|
|||||||
| Forums | Register | Forum Rules | Linux and Unix Links | Man Pages | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
[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
|
||||
|
||||
|
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
|
|||
|
|||
|
Thank you.
Its working. |
| Sponsored Links | ||
|
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|