Find and Replace based on values in an file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find and Replace based on values in an file
# 1  
Old 12-14-2010
Question 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
... ...


The question is how can I take the second file as input and do the necessary replacements in my first file instead of running individual replace commands?

Thx,
Guss
# 2  
Old 12-14-2010
Code:
 
cat second_file | while read line
do
    old=${line%% *}
    new=${line##* }
    sed -i "s/$old/$new/g" first_file
done

# 3  
Old 12-14-2010
Thanks for your solution Anurag. A minor question though; what if I were to have tab de-limiters instead of space, how do I incorporate that?

-Guss
# 4  
Old 12-14-2010
Use TAB space instead of single space
Code:
old=${line%%<TAB>*}
new=${line##*<TAB>}

This User Gave Thanks to anurag.singh For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find minimum and maximum values based on column with associative array

Hello, I need to find out the minimum and maximum values based on specific column, and then print out the entire row with the max value. Infile.txt: scf6 290173 290416 . + X_047241 T_00113118-1 scf6 290491 290957 . + X_047241 T_00113118-2 scf6 290898 290957 . + X_047241 T_00113119-3 scf6... (2 Replies)
Discussion started by: yifangt
2 Replies

2. Shell Programming and Scripting

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: DT:3 foo_err DT:34 bar_frr... (8 Replies)
Discussion started by: aydj
8 Replies

3. Shell Programming and Scripting

Find and replace values using a list from a file

Hello Guys. I have a very big file where I need to change some values according a list on other file. The following file is were I have the values to be changed. The field where I need to replace the values is File_Nb : File1.txt Obs_Report_Result : # ===== (5)... (19 Replies)
Discussion started by: jiam912
19 Replies

4. Shell Programming and Scripting

Find a blank field and replace values to NA

Hi All, i have a file like col1 col2 col3 13 24 NA 12 13 14 11 12 13 14 22 NA 18 26 NA in this file if i found "NA" other values in the line are also replace by NA Could you help me! (7 Replies)
Discussion started by: Shenbaga.d
7 Replies

5. Shell Programming and Scripting

Find and count unique date values in a file based on position

Hello, I need some sort of way to extract every date contained in a file, and count how many of those dates there are. Here are the specifics: The date format I'm looking for is mm/dd/yyyy I only need to look after line 45 in the file (that's where the data begins) The columns of... (2 Replies)
Discussion started by: ronan1219
2 Replies

6. Shell Programming and Scripting

Find and replace many values

Dear Friends, I did the same question before in other thread, but I want to explain a little better my request. I am looking for a way how to find and replace a values in two files using a reference a file where are the key to replace. Basically, I want to keep a copy of the original file... (1 Reply)
Discussion started by: jiam912
1 Replies

7. UNIX for Dummies Questions & Answers

Find and Replace based on a list

Hello, I have two files 'Master' and 'Rename'. Rename has two columns, the first containing old names and the second new names. I want to replace the old names in the 'Master' file with the new names. I ran the following and it does not seem to work. What needs to be done differently? Thanks,... (1 Reply)
Discussion started by: Gussifinknottle
1 Replies

8. Shell Programming and Scripting

Find and replace duplicate column values in a row

I have file which as 12 columns and values like this 1,2,3,4,5 a,b,c,d,e b,c,a,e,f a,b,e,a,h if you see the first column has duplicate values, I need to identify (print it to console) the duplicate value (which is 'a') and also remove duplicate values like below. I could be in two... (5 Replies)
Discussion started by: nuthalapati
5 Replies

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