Replace Strings with sed or awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace Strings with sed or awk
# 1  
Old 03-15-2010
Replace Strings with sed or awk

Hello i need some help with the usage of sed.

Situation : 2 textfiles, file.in , file.out
In the first textfile which is called file.in are the words for the substitution.
Every word is in a new-line like :
Code:
Firstsub
Secondsub
Thridsub
...

In the second textflie wich is called file.out is some text and in every line the word SUBSTITUDE exisist.
e.G. :
Code:
Lorem ipsum dolor SUBSTITUDE
Ipsum Lorem dolor color SUBSTITUDE
....

I want to replace the words SUBSTITUDE with the wordlist of the first textfile.

The output should look like this:
Code:
Lorem ipsum dolor Firstsub
Ipsum Lorem dolor color Secondsub
....

Could anyone help me by the syntax of sed or awk to do this job ?

Thx
Bruce

Last edited by vgersh99; 03-15-2010 at 11:05 AM.. Reason: code tags, please!
# 2  
Old 03-15-2010
Code:
nawk 'FNR==NR{f1[FNR]=$0;next} /SUBSTITUDE/{sub("SUBSTITUDE", f1[++i])}1' file.in file.out

# 3  
Old 03-16-2010
Here is the solution in sed ,

Code:
array=(`grep . file.in`)
i=0;
while read line
do
sub=`sed -r "s/SUBSTITUDE/${array[$i]}/g" <<< $line`
echo $sub >>newfile
let i=$i+1
done < "file.out"

The file "newfile" will contain the replaced contents .
# 4  
Old 03-16-2010
Thank you for the solution in sed karthigayan, i will try out this method too Smilie
# 5  
Old 03-16-2010
Quote:
Originally Posted by karthigayan
Here is the solution in sed ,

Code:
array=(`grep . file.in`)
i=0;
while read line
do
sub=`sed -r "s/SUBSTITUDE/${array[$i]}/g" <<< $line`
echo $sub >>newfile
let i=$i+1
done < "file.out"

The file "newfile" will contain the replaced contents .

Just a heads up, since we don't know exactly what the data in file.in looks like, if any of it contains characters which are special in sed's substitution command's replacement field, this approach will choke. All it takes is the presence of an "&", "\", or "/".

I'm not saying this approach will not work, but you need to keep this in mind. It depends on what assurances you have regarding the data's content.

Regards,
Alister
# 6  
Old 03-17-2010
Try:
Code:
awk '{getline x<f; sub(/SUBSTITUDE/,x)}1' f=file.in file.out

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 lossless replace strings using sed?

Hello, I would like to replace all occurencies of long data types by others (coresponding int) using 'sed' in the extensive source code of a software package written for 32 bit CPUs. I must use regular expressions to avoid wrong replacements like s/unsigned]+long/ulong/gLeaving out... (2 Replies)
Discussion started by: Mick P. F.
2 Replies

2. UNIX for Beginners Questions & Answers

sed find 2 strings and replace one

Hi Everyone, I want to find this 2 strings in a single line a file and replace the second string. this is the line i need to find <param name="user" value="CORE_BI"/> find user and CORE_BI and replace only CORE_BI with admin so finally the line should look like this. <param... (5 Replies)
Discussion started by: shajay12
5 Replies

3. Shell Programming and Scripting

sed Find and Replace Text Between Two Strings or Words

I am looking for a sed in which I can recognize all of the text in between two indicators and then replace it with a place holder. For instance, the 1st indicator is a list of words "no|noone|havent" and the 2nd indicator is a list of punctuation ".|,|!".From a sentence such as "noone... (3 Replies)
Discussion started by: owwow14
3 Replies

4. Programming

How to replace the complex strings from a file using sed or awk?

Dear All, I am having a requirement to find the difference between 2 files and generate a discrepancy report out of it as an html page. I prefer using diff -y file1 file2 since it gives user friendly layout to know any discrepancy in the record and unique records among the 2 file. Here's how it... (12 Replies)
Discussion started by: Badhrish
12 Replies

5. Shell Programming and Scripting

Using awk to replace strings

Hi.. I have a file that has the following content : abc 213 24 213 pqr 456#34 678 xyz 213 45%213 i need to write an awk script that will replace the second 213 in all the lines, if it is present. The IFS can not be specified and can be random. The number of lines in the file and the... (5 Replies)
Discussion started by: Hermione Grange
5 Replies

6. Shell Programming and Scripting

Using sed to replace strings if NOT found

Dear expert, I need an urgent help. I would like to update my /etc/ntp.conf file using sed. 1) if script find this string "127.127.1.0" then add the lone below #server 127.127.1.0 2) is script find this string "fudge 127.127.1.0 stratum 10" then add #fudge 127.127.1.0 stratum 10 ... (7 Replies)
Discussion started by: lamoul
7 Replies

7. Shell Programming and Scripting

Using SED with variable to replace strings in while loop

Hi guys, Hi have this input (Menu.xml)<?xml version="1.0" encoding="ISO-8859-1"?> <breakfast_menu> <food> <name>Berry-Berry Belgian Waffles</name> <price>$8.95</price> <calories>900</calories> </food> <food> <name>French Toast</name> ... (6 Replies)
Discussion started by: cgkmal
6 Replies

8. Shell Programming and Scripting

Using sed to replace two different strings?

Hey everyone! Simple question - I am trying to use sed to replace two different strings. As it stands I can implement this as: sed -i 's/TIMEOUT//g' sed -i 's/null//g' And it works. However, is it possible to shrink that down into a single command? Will there be any performance benefits? (3 Replies)
Discussion started by: msarro
3 Replies

9. Shell Programming and Scripting

Help with sed search&replace between two strings

Hi, i read couple of threads here on forum, and googled about what bugs me, yet i still can't find solution. Problem is below. I need to change this string (with sed if it is possible): This is message text that is being quoted to look like this: This is message text that is being quotedI... (2 Replies)
Discussion started by: angrybb
2 Replies

10. Shell Programming and Scripting

replace two character strings by two variables with sed command

Hello, I want to writte a script that replace two character strings by two variables with the command sed butmy solution doesn't work. I'm written this: sed "s/TTFactivevent/$TTFav/g && s/switchSLL/$SLL/g" templatefile. I want to replace TTFactivevent by the variable $TTFav, that is a... (4 Replies)
Discussion started by: POPO10
4 Replies
Login or Register to Ask a Question