LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to replace string pattern with multi-line text in bash script? (http://www.linuxquestions.org/questions/linux-newbie-8/how-to-replace-string-pattern-with-multi-line-text-in-bash-script-212983/)

brumela 08-03-2004 01:00 PM

How to replace string pattern with multi-line text in bash script?
 
hi,

I want to replace string pattern with multi-line text in bash script, for example

somefile.txt contains pattern "insert"

and I want to replace "insert" with some text:
line 1
line 2
line 3

I tried with sed but, I'm not sure If I can do that with sed?!?

regards brumela

nhs 08-03-2004 01:48 PM

Try something along the lines of:

sed 's/insert/line 1\nline2\nline3/' <infile >outfile

brumela 08-03-2004 03:58 PM

:D it works, thanks for tip

4dirk1 04-20-2011 03:29 AM

Quote:

Originally Posted by nhs (Post 1086201)
Try something along the lines of:

sed 's/insert/line 1\nline2\nline3/' <infile >outfile

Hi Sir,

I have a similar issue, instead of replace the string with multiple lines, I need to replace a keyword in an xml file with the contents of a txt file containing multiple lines. the xml file has a keyowrd 'kw01', and i want to replace this string with the contents of a file named fatal_alerts.txt. is this possible via sed? i badly need this. TIA!

keyword to be replaced:
kw01

fatal_alerts.txt contents:
RAISEDATTIME
--------------------
DESCRIPTION
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
18-APR-2011 06:20:32
Fatal error in Application GATE:
<Error code: 68
Caught by: CTCS_BatchSchedulerBO.HandleException
Raise by: CTCS_FTOServerBTM.ValidateMessageCount()
Message: For Interface: FTO:1315001 in File : 190107079531.txt
File Message Count Mismatch. File reported count: 447 actual message count 797.>

crts 04-20-2011 03:45 AM

Hi and welcome to LQ,

please do not resurrect such old threads. Start a new one instead and maybe link to it.

As for your problem, try this:
Code:

sed '/kw01/ {r fatal_alerts.txt
d}' file

It is important that you start a new line after fatal_alerts.txt.
I see a windows logo in your profile. So if you are trying to edit a windows file within linux there *might* be some line-ending issues
. Also, make sure that fatal_alerts has a blank line at the end of the file. Otherwise the line directly after kw01 might be concatenated with the last line of fatal_alerts.

4dirk1 04-20-2011 11:10 PM

Quote:

Originally Posted by crts (Post 4330280)
Hi and welcome to LQ,

please do not resurrect such old threads. Start a new one instead and maybe link to it.

As for your problem, try this:
Code:

sed '/kw01/ {r fatal_alerts.txt
d}' file

It is important that you start a new line after fatal_alerts.txt.
I see a windows logo in your profile. So if you are trying to edit a windows file within linux there *might* be some line-ending issues
. Also, make sure that fatal_alerts has a blank line at the end of the file. Otherwise the line directly after kw01 might be concatenated with the last line of fatal_alerts.

sir i tried the command you suggested but still results to a garbled error:

Quote:

bash-2.05$ sed '/kw01/ {r fatal_alerts.txt
d}' template.xml > test.xml
sed: command garbled: d}
TIA SIR! really really appreciate the help!

crts 04-21-2011 06:56 AM

I tried the command with the sample data you provided and it worked. Not sure what went wrong.
You could try:
Code:

sed -r '/kw01/ {r fatal_alerts.txt
d;};' file > newfile

What is the exact keyword you are tryinhg to replace?
Does it contain characters like '/'?
Are you trying to edit a file that was created under windows originally?
I see that you use an old version of bash. Which version is 'sed' that you are using?
Post the output of:
Code:

sed --version
od -a template.xml|grep 'nl'|head



All times are GMT -5. The time now is 10:19 AM.