LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 03-14-2011, 08:45 AM   #1
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Rep: Reputation: 6
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.
 
Old 03-14-2011, 08:50 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,508

Rep: Reputation: 1894Reputation: 1894Reputation: 1894Reputation: 1894Reputation: 1894Reputation: 1894Reputation: 1894Reputation: 1894Reputation: 1894Reputation: 1894Reputation: 1894
Check out your favourite tutorial and look up the 'N' option
 
Old 03-14-2011, 09:21 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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
 
1 members found this post helpful.
Old 03-16-2011, 10:34 AM   #4
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Original Poster
Rep: Reputation: 6
Quote:
Originally Posted by David the H. View Post
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.
 
  


Reply

Tags
regular expressions, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Help with sed regex to match words via a pattern. logar0 Linux - Newbie 3 10-24-2010 04:33 PM
How to use sed to delete all lines before the first match of a pattern? C_Blade Linux - Newbie 9 05-01-2010 04:18 AM
help extracting a matching pattern and next lines of match madvicious Programming 8 09-13-2009 01:01 AM
simple pattern match with awk, sed alenD Linux - Newbie 10 03-10-2008 02:31 PM
sed display line after pattern match inonzi_prowler Linux - Software 3 02-19-2007 01:47 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration