LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-03-2004, 01:00 PM   #1
brumela
LQ Newbie
 
Registered: May 2004
Location: MB, Slovenia
Distribution: Fedora 1, Redhat 8, White box
Posts: 21

Rep: Reputation: 15
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
 
Old 08-03-2004, 01:48 PM   #2
nhs
Member
 
Registered: Aug 2003
Location: Edinburgh, Scotland
Distribution: Gentoo
Posts: 246

Rep: Reputation: 30
Try something along the lines of:

sed 's/insert/line 1\nline2\nline3/' <infile >outfile
 
Old 08-03-2004, 03:58 PM   #3
brumela
LQ Newbie
 
Registered: May 2004
Location: MB, Slovenia
Distribution: Fedora 1, Redhat 8, White box
Posts: 21

Original Poster
Rep: Reputation: 15
it works, thanks for tip
 
Old 04-20-2011, 03:29 AM   #4
4dirk1
LQ Newbie
 
Registered: Apr 2011
Posts: 3

Rep: Reputation: 0
Quote:
Originally Posted by nhs View Post
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.>
 
Old 04-20-2011, 03:45 AM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
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.
 
Old 04-20-2011, 11:10 PM   #6
4dirk1
LQ Newbie
 
Registered: Apr 2011
Posts: 3

Rep: Reputation: 0
Quote:
Originally Posted by crts View Post
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!

Last edited by 4dirk1; 04-20-2011 at 11:19 PM.
 
Old 04-21-2011, 06:56 AM   #7
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
escape string in bash script so it can be used in command line BuckRogers01 Linux - Software 15 08-12-2010 09:38 AM
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 06:02 AM
Bash script text line into an array toolshed Programming 1 06-13-2005 05:49 PM
bash-script: output text between two ocurrences of a specific string isl01jbe Programming 1 06-17-2004 02:36 PM
Find string pattern in directory of text files magnum818 Linux - Newbie 2 10-15-2003 08:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration