sed find and replace multiple lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed find and replace multiple lines
# 1  
Old 04-03-2009
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  
Old 04-03-2009
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  
Old 04-03-2009
what have you done so far?
# 4  
Old 04-03-2009
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  
Old 04-03-2009
Quote:
Originally Posted by supersimha
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  
Old 04-03-2009
Quote:
Originally Posted by siquadri
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  
Old 04-04-2009
Try this:

Quote:
perl -i -ne 's/I am the string/replace string /; print; ' file
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to find=grep or maybe sed/awk for multiple lines of text?

Hi, I am running the following: PASS="username/password" sqlplus -s << EOF | grep -v "^$" $PASS set feedback off set heading off set termout off select name from v\$database ; exit EOF Which gives ERROR: ORA-28002: the password will expire within 5 days PSMP1 (1 Reply)
Discussion started by: newbie_01
1 Replies

2. Shell Programming and Scripting

Sed/awk/perl command to replace pattern in multiple lines

Hi I know sed and awk has options to give range of line numbers, but I need to replace pattern in specific lines Something like sed -e '1s,14s,26s/pattern/new pattern/' file name Can somebody help me in this.... I am fine with see/awk/perl Thank you in advance (9 Replies)
Discussion started by: dani777
9 Replies

3. Shell Programming and Scripting

Replace multiple lines through sed

Hi All, I have a input file as sample below <this is not starting of file> record line1 line2 line3 end line4 line5 record line6 line7 line8 my requirement is this, i want to select a pattern between first record and end, whatever is written between first record and end. and... (0 Replies)
Discussion started by: adgangwar
0 Replies

4. Shell Programming and Scripting

using sed to find and replace multiple numbers

I have looked around and there are several examples of how to use sed, but I don't think any of them help me very much with what I am trying to do. I have a text file like this.... 1! SRCNAM = 00001 ! 1! X = 50.0000, 0.0000,... (10 Replies)
Discussion started by: mercury.int
10 Replies

5. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

6. Shell Programming and Scripting

Find and replace multiple lines

I have a section of text in file A, see below # falkdjf lkjadf lkjadf lkajdf lkajdf lkajdf lkjadf lkjadf 234.234.2.234 lkjlkjlk 234.234.3.234 # Only the first line with "# falkdjf lkjadf lkjadf" is unique in the file. The new section that I want to overwrite the old section above is in... (1 Reply)
Discussion started by: jyang72211
1 Replies

7. Shell Programming and Scripting

SED multiple find and replace

Hi, searched through the forums and not really found what I am looking for. I am a bit of novice when it comes to anything above basic scripting and not even that when it comes to the sed command. I have been reading the tutorials online but still struggling to get what I need :wall: ... (10 Replies)
Discussion started by: colinwilson1303
10 Replies

8. Shell Programming and Scripting

Find 5 lines and replace with 18 line in sql file where it contains multiple blocks.

My sql file xyz_abc.sql in this file there are multiple sql block in this block I need to find the following block rem Subset Rows (&&tempName.*) CREATE VIEW &&tempName.* AS SELECT * FROM &&tempName.* WHERE f is not null and replace with following code rem Subset Rows... (9 Replies)
Discussion started by: Zaheer.mic
9 Replies

9. Shell Programming and Scripting

Replace multiple lines between tags using sed

I have a file example.txt with content look like this: <TAG> 1 2 3 </TAG> and I use a sed command to replace everything between <TAG></TAG> as below: sed -e 's/\(<TAG>\)*\(<.*\)/something/g' example.txt > example.txt.new But unfortunately, the command failed to replace as i want, it... (23 Replies)
Discussion started by: dollylamb
23 Replies

10. Shell Programming and Scripting

using sed command to replace multiple lines

the file contains the follwoing lines /* * Copyright (C) 1995-1996 by XXX Corporation. This program * contains proprietary and confidential information. All rights reserved * except as may be permitted by prior written consent. * * $Id: xxx_err.h,v 1.10 2001/07/26 18:48:34 zzzz $ ... (1 Reply)
Discussion started by: radha.kalivar
1 Replies
Login or Register to Ask a Question