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
# 1  
Old 12-16-2013
Find and replace string based on entries on another file

I have a file1 with different with multiple fields and records
File2 has 2 fields.
I want to find and replace strings in file1 based on file2 values (I Want an exact match i.e. for example: when searching for DT:3, Substr of DT:34 should not be matched)

File2:
Code:
DT:3 foo_err
DT:34 bar_frr
DT:47 foo_bar
TK:22 gree

file1:
Code:
he will come to DT:34 and stay there. I will meet her in DT:3 
gree will see me there

Desired Output:

Code:
he will come to bar_frr and stay there. I will meet her in foo_err
TK:22 will see me there

# 2  
Old 12-16-2013
Question:
Can your entries for "DT:3" become "DT:03"?

If not, then searching for "DT:3 " {note, space after the '3'} might work. But, I fear it will be a problem when at end of a line.

Another approach would be to somehow make sure that you change all with two-digit numbers prior to single-digit numbers. Execute for "DT-34" before "DT-3".
# 3  
Old 12-16-2013
Cannot change DT:3 to DT:03 because file1 is has many entries.
# 4  
Old 12-16-2013
for given input this will work

Code:
$ awk 'FNR==NR{A[$1]=$2;next}{for(i in A){if($0~i){sub(i,A[i],$0)}else if($0~A[i]){sub(A[i],i,$0)}}}1' file2 file1

he will come to bar_frr and stay there. I will meet her in foo_err 
TK:22 will see me there

# 5  
Old 12-16-2013
Quote:
Originally Posted by Akshay Hegde
for given input this will work

Code:
$ awk 'FNR==NR{A[$1]=$2;next}{for(i in A){if($0~i){sub(i,A[i],$0)}else if($0~A[i]){sub(A[i],i,$0)}}}1' file2 file1

he will come to bar_frr and stay there. I will meet her in foo_err 
TK:22 will see me there

Sorry for my Omission TK:22 should also be replaced (every occurrence should be replaced).

output:
Code:
he will come to bar_frr and stay there. I will meet her in foo_err 
gree will see me there

# 6  
Old 12-16-2013
Quote:
Originally Posted by aydj
Sorry for my Omission TK:22 should also be replaced (every occurrence should be replaced).

output:
Code:
he will come to bar_frr and stay there. I will meet her in foo_err 
gree will see me there


Quote:
Originally Posted by aydj
I have a file1 with different with multiple fields and records
File2 has 2 fields.
I want to find and replace strings in file1 based on file2 values (I Want an exact match i.e. for example: when searching for DT:3, Substr of DT:34 should not be matched)

File2:
Code:
DT:3 foo_err
DT:34 bar_frr
DT:47 foo_bar
TK:22 gree

file1:
Code:
he will come to DT:34 and stay there. I will meet her in DT:3 
gree will see me there

Desired Output:

Code:
he will come to bar_frr and stay there. I will meet her in foo_err
TK:22 will see me there

You shown desired output this right ?

Try this then

Code:
$ awk 'FNR==NR{A[$1]=$2;next}{for(i in A)if($0~i)sub(i,A[i],$0)}1' file2 file1

he will come to bar_frr and stay there. I will meet her in foo_err 
gree will see me there

This User Gave Thanks to Akshay Hegde For This Post:
# 7  
Old 12-17-2013
Pls I'm still having substring replaced i.e. DT:34 replaced to foo_err4, any ideas?

---------- Post updated 12-17-13 at 04:37 AM ---------- Previous update was 12-16-13 at 12:18 PM ----------

I have tried to sort file2 but still not working.

Last edited by aydj; 12-17-2013 at 04:57 AM..
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