Find and replace values using a list from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and replace values using a list from a file
# 15  
Old 11-26-2013
sed, something like this?
Code:
sed "$( while read a b
do
  echo "s/\\<$a\\>/$b/"
done <file2.txt )" file1.txt

This User Gave Thanks to DGPickett For This Post:
# 16  
Old 11-26-2013
An attempt to avoid string ops and full-line matches
Code:
awk 'NR==FNR {a[$1]=$2; next}
$1~/File_Nb/ && ($NF in a) {sub($NF,a[$NF])}
1' file2 file1

These 2 Users Gave Thanks to MadeInGermany For This Post:
# 17  
Old 11-27-2013
@MadeInGermany: good point! But then, shouldn't sub($NF,a[$NF])read sub($NF, a[$NF], $NF) to avoid the full line match?
This User Gave Thanks to RudiC For This Post:
# 18  
Old 11-27-2013
Thanks to all for your help
# 19  
Old 11-27-2013
Quote:
Originally Posted by RudiC
@MadeInGermany: good point! But then, shouldn't sub($NF,a[$NF])read sub($NF, a[$NF], $NF) to avoid the full line match?
Unfortunately, if the sub() target is a field string $NF, it causes a reformatting of $0 (just like $NF="string").

---------- Post updated at 09:37 AM ---------- Previous update was at 09:27 AM ----------

Quote:
Originally Posted by DGPickett
sed, something like this?
Code:
sed "$( while read a b
do
  echo "s/\\<$a\\>/$b/"
done <file2.txt )" file1.txt

Interesting idea. But should at least match /File_Nb/ somewhere
Code:
sed "$( while read a b
do
  echo '/File_Nb/ s/\<$a\>/$b/'
done <file2.txt )" file1.txt

This User Gave Thanks to MadeInGermany For This Post:
# 20  
Old 11-27-2013
Writing scripts that write scripts is neato!
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find common values in python list in ordered format

Hello All, There are various codes available to find the intersection between two sets in python. But my case is the following: I want to find the continual common pattern in different lists compared to list1. (i have underlined the longest common patterns in set a and set b) a = 2, 3, 5,... (1 Reply)
Discussion started by: Zam_1234
1 Replies

2. Shell Programming and Scripting

How to find the X highest values in a list depending on the values of another list with bash/awk?

Hi everyone, This is an exemple of inpout.txt file (a "," delimited text file which can be open as csv file): ID, Code, Value, Store SP|01, AABBCDE, 15, 3 SP|01, AABBCDE, 14, 2 SP|01, AABBCDF, 13, 2 SP|01, AABBCDE, 16, 3 SP|02, AABBCED, 15, 2 SP|01, AABBCDF, 12, 3 SP|01, AABBCDD,... (1 Reply)
Discussion started by: jeremy589
1 Replies

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

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

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

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

7. 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
Login or Register to Ask a Question