LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   use sed in bash to match pattern contained in 2 lines (http://www.linuxquestions.org/questions/linux-newbie-8/use-sed-in-bash-to-match-pattern-contained-in-2-lines-868477/)

ghantauke 03-14-2011 08:45 AM

use sed in bash to match pattern contained in 2 lines
 
Hi,

I have a file called test. It has the following contents.
Code:

hey
there you

I want the output to be.
Code:

replaced you
I am trying to use the sed command to replace every occurance of "hey newline there" with "replaced". I tried the following naive apporach.
Code:

sed 's/hey\nthere/replace/' test
This gives a result containing the same data as the test file.

Any kind of help would be appreciated.

grail 03-14-2011 08:50 AM

Check out your favourite tutorial and look up the 'N' option

David the H. 03-14-2011 09:21 AM

You almost have it actually. It's just that sed only processes single lines by default, and is not particularly well-suited for multi-line edits. But as grail mentioned, it is possible. In particular, the "N" command can be used to append the next line to the one currently in the pattern buffer, allowing you to edit them both as a single line.

I've only just started to really learn how to do it myself. A lot of it is not particularly intuitive. Here are the basics of it.

Code:

$ echo -e "hey\nthere you" | sed '/hey/ {N ; s/hey\nthere/replaced/}'
replaced you

In the above, when the line in the pattern buffer contains the pattern specified in /hey/, it will apply the following {..} bracket-grouped commands on it. The brackets are probably not really necessary in this case, but I think it's better to be safe and use them anyway.

Inside brackets we start with the N command, which adds the next line to the "hey" line, separating them with a newline character.

A semicolon ; is used to start a new command.

Then finally we can use the standard s/// replacement pattern on the combined line, with \n used to match the newline character. The output will be the modified line.

Here are a few useful sed references for you. The first one in particular explains how to do things like this.

http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

ghantauke 03-16-2011 10:34 AM

Quote:

Originally Posted by David the H. (Post 4290246)
You almost have it actually. It's just that sed only processes single lines by default, and is not particularly well-suited for multi-line edits. But as grail mentioned, it is possible. In particular, the "N" command can be used to append the next line to the one currently in the pattern buffer, allowing you to edit them both as a single line.

I've only just started to really learn how to do it myself. A lot of it is not particularly intuitive. Here are the basics of it.

Code:

$ echo -e "hey\nthere you" | sed '/hey/ {N ; s/hey\nthere/replaced/}'
replaced you

In the above, when the line in the pattern buffer contains the pattern specified in /hey/, it will apply the following {..} bracket-grouped commands on it. The brackets are probably not really necessary in this case, but I think it's better to be safe and use them anyway.

Inside brackets we start with the N command, which adds the next line to the "hey" line, separating them with a newline character.

A semicolon ; is used to start a new command.

Then finally we can use the standard s/// replacement pattern on the combined line, with \n used to match the newline character. The output will be the modified line.

Here are a few useful sed references for you. The first one in particular explains how to do things like this.

http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

Thanks for the detailed explanation. Reps to you.


All times are GMT -5. The time now is 12:07 AM.