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
# 8  
Old 11-26-2013
Please note that an awk program never modifies the content of original input file. So you have to redirect the output to a temporary file and rename it back to original file:
Code:
awk ... file1.txt > tmp; mv tmp file1.txt

This User Gave Thanks to Yoda For This Post:
# 9  
Old 11-26-2013
In the output i got:
File_Nb : 529

I would like to get
Code:
    File_Nb             : 529

Same format of the original file

Thansk for your help Smilie

---------- Post updated at 02:42 PM ---------- Previous update was at 02:40 PM ----------

Quote:
Originally Posted by Yoda
Please note that an awk program never modifies the content of original input file. So you have to redirect the output to a temporary file and rename it back to original file:
Code:
awk ... file1.txt > tmp; mv tmp file1.txt

Yoda Thanks a lot for your help..
Yes I did it but the format as changed and I would like to keep the same format of the original file...
# 10  
Old 11-26-2013
I'm sorry, I misread what you requested.

Replace $NF=a[$NF] with sub(/:[ ].*$/,": "a[$NF])
Code:
awk 'NR==FNR{a[$1]=$2; next} /File_Nb/{sub(/:[ ].*$/,": "a[$NF])}1' file2 file1

This User Gave Thanks to Yoda For This Post:
# 11  
Old 11-26-2013
Give this a shot (based on Subbeh's proposal):
Code:
awk 'NR==FNR{gsub(/ /,"",$2); a[$1]=$2; next} /File_Nb/{gsub(/ /,"",$NF); $NF=" "a[$NF]}1' file2 FS=":" OFS=":" file1

This User Gave Thanks to RudiC For This Post:
# 12  
Old 11-26-2013
@Rudi: What are the gsub() for?
# 13  
Old 11-26-2013
You are right, the first one is not needed. You need the second one because FS=":" will leave the space in so $NF will not be found in array a. So this should do
Code:
awk 'NR==FNR{a[$1]=$2; next} /File_Nb/{gsub(/ /,"",$NF); $NF=" "a[$NF]}1' file2 FS=":" OFS=":" file

This User Gave Thanks to RudiC For This Post:
# 14  
Old 11-26-2013
Quote:
Originally Posted by RudiC
You are right, the first one is not needed. You need the second one because FS=":" will leave the space in so $NF will not be found in array a. So this should do
Code:
awk 'NR==FNR{a[$1]=$2; next} /File_Nb/{gsub(/ /,"",$NF); $NF=" "a[$NF]}1' file2 FS=":" OFS=":" file

Many Thanks to all Smilie
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