sed to search and replace - iterating on a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed to search and replace - iterating on a file
# 1  
Old 05-19-2014
sed to search and replace - iterating on a file

Hi,

I am new to sed scriping but do have some basic scripting skills. I have a properties file props.conf with name/value pairs written in it. Sample is

Code:
 
Name1=test
Name2=notest

Then I have a template file (can be xml or simple text) which will have 'keys' embedded in it. Keys can only be from those mentioned (not all but some) in props.conf file. Sample template.txt file is as below

Code:
 
My name is : Name1
I live in OZ
My last name is : Name2

I want to have script which will replace all the 'keys' in the template file with the values mentioned in props.conf file. So output should look like :

Code:
 
My name is : test
I live in OZ
My last name is : no test

Also some of the values in props.conf file will have special characters (such as forward slash) in them so they should be replaced in output file as it as.

I have been trying to work it out but cannot. Following is script I have got but it is no where close to what I need to get.
Code:
 
IFS="="
 
while read f1 f2
do
  sed "s!\#\#\*$f1\*\#\#!$f2!g" template.txt > template.txt
done < props.conf

Please can someone help. Will be appreciated :-)
# 2  
Old 05-19-2014
try
Code:
nawk 'NR == FNR{a[$2] = $1; next}
  {for(x in a)
    {gsub(a[x],x)};
    print}' FS='\=' props.conf file.xml

# 3  
Old 05-19-2014
This one seems be doing the job for txt file but have not tested with xml files. yet.
Code:
IFS="="
while read f1 f2
do
  sed "s!\#\#\*$f1\*\#\#!$f2!g" template.txt > template1.txt
  mv template.txt template1.txt
done < props.conf


Last edited by Franklin52; 05-20-2014 at 08:04 AM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to search and replace string in column in file with command sed?

how to search and replace string in column in file with command sed or other search "INC0000003.in" and replace column 4 = "W" $ cat file.txt INC0000001.in|20150120|Y|N|N INC0000002.in|20150120|Y|N|N INC0000003.in|20150120|Y|N|N INC0000004.in|20150120|Y|N|Noutput... (4 Replies)
Discussion started by: ppmanja3
4 Replies

2. Shell Programming and Scripting

Need help with search and replace using SED

Hi guys, thanks for accepting me in your forum .. I am trying to clean some hacked PHP files using SSH .. I am using this command: find . -type f -print0 | xargs -0 sed -i '/god_mod/d' <?php ... (3 Replies)
Discussion started by: wisam74us
3 Replies

3. Shell Programming and Scripting

Sed emptying file when i use for search replace operation

Hi Friends I am new to sed programming , i found that the below code can search for the $ToSearch and Replace it with $ToReplace ( $ToSearch and $ToReplace are my variables in my script ) sed "s/$ToSearch/$ToReplace/" $file > $output mv $output $file In testing the script i found that... (3 Replies)
Discussion started by: rakeshkumar
3 Replies

4. Shell Programming and Scripting

Sed - search and replace help.

Hi everyone, basically I am been cleaning data by using simple sed commands So what i have below has been working for me. sed 's/="//g' trade.csv > tradeb.csv sed 's/"//g' tradeb.csv > trade2.csv but now i don't want to remove all the quotes just the ones if i encounter this ... (1 Reply)
Discussion started by: raz0r
1 Replies

5. UNIX for Dummies Questions & Answers

How to use 'sed' to search and replace?

Hello - I have a very large file in which a certain numbers are repeated. I find that using vi to edit the entire file is useless. How should i use sed to find a replace such as this text: To replace: 145.D25.D558 With: 215.22.45.DW I tried this command: sed... (4 Replies)
Discussion started by: DallasT
4 Replies

6. Shell Programming and Scripting

sed search and replace

hi, im new for sed, anyone can help me to these in sed command my output file.txt "aaa",a1,bbb "ddd",a1,ccc "eee",a1,www need to change a1, to "a1"," output i need "aaa","a1","bbb "ddd","a1","ccc "eee","a1","www thanks in advance fsp (2 Replies)
Discussion started by: fspalero
2 Replies

7. UNIX for Dummies Questions & Answers

How to search and replace a particular line in file with sed command

Hello, I have a file and in that, I want to search for a aprticular word and then replace another word in the same line with something else. Example: In file abc.txt, there is a line <host oa_var="s_hostname">test</host> I want to search with s_hostname text and then replace test with... (2 Replies)
Discussion started by: sshah1001
2 Replies

8. UNIX for Dummies Questions & Answers

Search/Replace with Sed

Is there a way to use the sed command to 1) search a specified pattern 2) in the line where that pattern is found, replace from character N to character N+4 with a new 4-character string. Thks in advance! (5 Replies)
Discussion started by: mvalonso
5 Replies

9. UNIX for Dummies Questions & Answers

sed search and replace

Hello Folks, Anyone know how I can replace this line in file.xml <oacore_nprocs oa_var="s_oacore_nprocs">8</oacore_nprocs> with this line <oacore_nprocs oa_var="s_oacore_nprocs">1</oacore_nprocs> using sed or awk ? Thanks for your time. Cheers, Dave (7 Replies)
Discussion started by: d__browne
7 Replies

10. Shell Programming and Scripting

Search and replace sed or tr

Hi folks, I need to search and replace specific text in a file and replace it. I have a text file that does not have any newlines or carriage returns. All newlines have been removed. Here is what I need to do. Find the exact string “DH” (quotes included) and replace it with \n”DH” (basically... (6 Replies)
Discussion started by: bridgeje
6 Replies
Login or Register to Ask a Question