Operating System - HP-UX
1836460 Members
2381 Online
110101 Solutions
New Discussion

Replacing text with multiple lines

 
SOLVED
Go to solution
Quin Hammes
Valued Contributor

Replacing text with multiple lines

Full 10 points for who ever solves this one for me.....

I have a text config file.
I want to replace any instance of the word SET with
"SET
TROUBLETICKET".

I tried sed and awk but they get confused with the carriage return between set and troubleticket which is required. Any ideas? I need to make this an automatic process because there is going to be around 800 hundred instances of SET in this file and we need to do this translation about once a month. Any help would be appreciated (along with ten points). Thank you.
4 REPLIES 4
Rodney Hills
Honored Contributor
Solution

Re: Replacing text with multiple lines

You can use sed as follows-
sed -e 's/SET/SET\^MTROUBLETICKET/'

Following the \, press ctl-v followed by the return-key.
There be dragons...
Quin Hammes
Valued Contributor

Re: Replacing text with multiple lines

I think I did it exaclt the way you said but it only spitted back TROUBLETICKET not
SET
TROUBLETICKET

any ideas?
Quin Hammes
Valued Contributor

Re: Replacing text with multiple lines

I figured it out, I did a "\" then a control M right away and it seemed to work. Thanks for the help.
Patrick Wallek
Honored Contributor

Re: Replacing text with multiple lines

Here is a little script that should do what you need to do. It is probably not the most efficient, but it works.
*******************************start script
#!/bin/sh

inputfile="/tmp/pww/file"
outputfile="/tmp/pww/file1"


outputfile1="/tmp/pww/file2"

cat $inputfile | awk '{ gsub(/SET/,"SET TROUBLE TICKET" ); print }' > $outputfile
cat $outputfile | awk '
{
if ( $1 != "SET" )
{ print $0 }
else
{ split($0,word," "); print word[1];print word[2],word[3] }
}' > $outputfile1

******************************end script

you could even do something at the end of the script to move the the output file to the inputfile name.

I hope the script makes sense. It is also attached to this reply.