![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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 |
|
||||
|
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 |
|
||||
|
Quote:
Check this Code:
nawk '$0 ~/findstring/ {print $0;getline;gsub(/replace string/,"replaced string");print}' datafile > datafile1
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|