The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replace multiple lines between tags using sed dollylamb Shell Programming and Scripting 23 06-18-2009 04:44 PM
replace multiple lines in multiple files unihp1 Shell Programming and Scripting 1 09-21-2008 09:47 AM
replace multiple lines in file nox Shell Programming and Scripting 2 08-18-2008 09:44 AM
using sed command to replace multiple lines radha.kalivar Shell Programming and Scripting 1 07-10-2007 11:36 AM
How can I replace multiple lines from different files ranga27 Shell Programming and Scripting 2 02-07-2007 08:57 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-03-2009
supersimha supersimha is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 2
sed find and replace multiple lines

I am new to linux and would like to modify the contents of a file preferably using a one line. The situation is as follows

<start>
some lines
"I am the string"
"replace string"
more lines here
<end>

In the above example,On encountering "I am the string", the "replace string "should be modified to say "string replaced"

i tried doing the following

sed -e 's/"I am the string"\n"replace string"/"I am the string"\n"string replaced"' myfile.txt

Also how do I make the modifications reflect in the file. its only diplaying at the terminal
  #2 (permalink)  
Old 04-03-2009
supersimha supersimha is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 2
i have a file say file.txt having following contents


Code:
<tags>match the string</tags>
<tags>match</tags>

later the file should be(preferably with the same file name"file.txt")


Code:
<tags>match the string</tags>
<tags>replaced</tags>

The replace must be done if it encounters the first line only.
Can you give me a single line command for this?

This seemed more precise

Thank you

Last edited by Yogesh Sawant; 04-03-2009 at 04:25 PM.. Reason: added code tags
  #3 (permalink)  
Old 04-03-2009
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
what have you done so far?
  #4 (permalink)  
Old 04-03-2009
ldapswandog ldapswandog is offline
Registered User
  
 

Join Date: Feb 2009
Location: Charlotte, NC
Posts: 101
sed only operates on the line contained in its buffer, so to work on multiple lines you need to read more lines into the buffer. Depending on what all you want to do there are flags for read ahead buffer and read behind buffers. Also sed just outputs to screen so you have to redirect the output to a new file, then copy or move the new file to the original file name. I would always make a backup of the original file prior to modifications.

Code:
#sed -e 's/"I am the string"\n"replace string"/"I am the string"\n"string replaced"' 
sed -e '/"I am the string"/N;s/"replace string"/"string replaced"/' myfile.txt > myfile_new.txt

  #5 (permalink)  
Old 04-03-2009
siquadri siquadri is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 44
Quote:
Originally Posted by supersimha View Post
I am new to linux and would like to modify the contents of a file preferably using a one line. The situation is as follows

<start>
some lines
"I am the string"
"replace string"
more lines here
<end>

In the above example,On encountering "I am the string", the "replace string "should be modified to say "string replaced"

i tried doing the following

sed -e 's/"I am the string"\n"replace string"/"I am the string"\n"string replaced"' myfile.txt

Also how do I make the modifications reflect in the file. its only diplaying at the terminal

Check this

Code:
nawk '$0 ~/findstring/ {print $0;getline;gsub(/replace string/,"replaced string");print}' datafile > datafile1

  #6 (permalink)  
Old 04-03-2009
siquadri siquadri is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 44
Quote:
Originally Posted by siquadri View Post
Check this

Code:
nawk '$0 ~/findstring/ {print $0;getline;gsub(/replace string/,"replaced string");print}' datafile > datafile1

In place of findstring use I am the String
  #7 (permalink)  
Old 04-04-2009
dennis.jacob dennis.jacob is offline Forum Advisor  
dj - the student
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 609
Try this:

Quote:
perl -i -ne 's/I am the string/replace string /; print; ' file
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:25 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0