Find and replace string based on entries on another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and replace string based on entries on another file
# 8  
Old 12-17-2013
Code:
# cat f2
DT:3 foo_err
DT:34 bar_frr
DT:47 foo_bar
TK:22 gree
# cat f1
he will come to DT:34 and stay there. I will meet her in DT:3
gree will see me there
he will come to DT:34 and stay there. I will meet her in DT:3
TK:22 will see me there
# awk 'FNR==NR{A[$1]=$2;next}{for(i=0;++i<=NF;) if($i in A) sub($i,A[$i],$0); else { for(j in A) { if(A[j]==$i) sub($i,j,$0) }}}1' f2 f1
he will come to bar_frr and stay there. I will meet her in foo_err
TK:22 will see me there
he will come to bar_frr and stay there. I will meet her in foo_err
gree will see me there
#

Or :
Code:
# awk 'NR==FNR{A[$1]=$2;B[$2]=$1;next}{for(i=0;++i<=NF;) {x=($i in A)?A[$i]:($i in B)?B[$i]:z;if(x!=z) sub($i,x,$0)}}1' f2 f1
he will come to bar_frr and stay there. I will meet her in foo_err
TK:22 will see me there
he will come to bar_frr and stay there. I will meet her in foo_err
gree will see me there


Last edited by ctsgnb; 12-17-2013 at 06:55 AM..
This User Gave Thanks to ctsgnb For This Post:
# 9  
Old 12-17-2013
As joeyg pointed out, end-of-lines might be problematic. Try a modification of Akshay Hegde's proposal:
Code:
awk 'FNR==NR{A[$1]=$2;next}{for(i in A)if($0~i"[^0-9]|$")sub(i,A[i],$0)}1' file1 file2

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find and replace a string in a text file

Dear all, I want to find all the "," in my text file and then replace the commas to a tab. I found a script online but I don't know how to modify the script for my case. Any one can help? Thank you. @echo off &setlocal set "search=%1" set "replace=%2" set "textfile=Input.txt" set... (2 Replies)
Discussion started by: forevertl
2 Replies

2. Shell Programming and Scripting

Perl script to read string from file#1 and find/replace in file#2

Hello Forum. I have a file called abc.sed with the following commands; s/1/one/g s/2/two/g ... I also have a second file called abc.dat and would like to substitute all occurrences of "1 with one", "2 with two", etc and create a new file called abc_new.dat sed -f abc.sed abc.dat >... (10 Replies)
Discussion started by: pchang
10 Replies

3. Shell Programming and Scripting

find string and replace with string in other file

Dear all, I need your help, I have file like this: file1:23456 01910964830098775635 34567 01942809546554654323 67589 26546854368698023653 09778 58716868568576876878 08675 86178546154065406546 08573 54165843543054354305 . .file2: 23456 25 34567 26 67589 27 (2 Replies)
Discussion started by: attila
2 Replies

4. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

5. Shell Programming and Scripting

bash script to find date based on search string for continuesly updating file

Hi All, I am very new to UNIX and I have tried this for a longtime now and unable to crack it.... There is a file that is continuously updating. I need to search for the string and find the date @ which it updated every day..... eg: String is "work started" The log entry is as below: ... (1 Reply)
Discussion started by: Nithz
1 Replies

6. Shell Programming and Scripting

How to find a certain string in a file and replace it with a value from another file using sed/awk?

Hi Everyone, I am new to this forum and new to sed/awk programming too !! I need to find particular string in file1(text file) and replace it with a value from another text file(file2) the file2 has only one line and the value to be replaced with is in the second column. file 1: (assert (=... (21 Replies)
Discussion started by: paramad
21 Replies

7. UNIX for Dummies Questions & Answers

Find and Replace based on values in an file

I have a file in which I want to do multiple find and replace of strings. For a single replace I can implement: sed -i 's/old/new/' <input_file> I have a second file that contains the old and the new values like the arbitrary example below: old new xyz pqr ab 756 rst pqr... (3 Replies)
Discussion started by: Gussifinknottle
3 Replies

8. Shell Programming and Scripting

Find and replace string from file which contains variable and path - SH

e.g. /home/$USER/.config replace it with "" (empty) Is this possible? I think you should play a bit with sharps ## and sed:b: (2 Replies)
Discussion started by: hakermania
2 Replies

9. Shell Programming and Scripting

find and replace a string in a file without the use of temp file

Hi - I am looking for a replacing a string in a in multiple *.sql files in directory with a new string without using a temporary file Normally I can use sed command as below for W in ls `FILE*.sql` do sed 's/OLD/NEW/g' $W > TEMPFILE.dat mv TEMPFILE.dat $W done But Here in my... (9 Replies)
Discussion started by: raghutapal
9 Replies

10. Shell Programming and Scripting

Find and replace based on other file

Hi All, I need an enlightenment. I have a file that some field need to be replaced. sample : fileA.txt abc,4#cik#221,text abc,4#kus#343,text ... ... what I need to replace is the "cik" and "kus" field to their fix value. I have another file : fileB.txt that has value of "cik and... (1 Reply)
Discussion started by: kunimi
1 Replies
Login or Register to Ask a Question