Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 12-09-2012, 10:26 PM   #1
Registered: Sep 2004
Distribution: PCLinuxOS
Posts: 59

Rep: Reputation: 15
sed search and replace question

I want to search for a pattern across two consecutive lines and use part of that pattern to change part of the second line. I haven't been able to find anything about sed that would show me how to do it.

Everything I have found shows how to merge lines and add new blank lines but nothing shows how to do what I want.

I can do this all day long in Vim but I want to do it in the bash shell using sed or sed & awk or something else because I have several dozen files to do this on and opening them in Vim is painfully slow.

This single line below will find a pattern across two consecutive lines then put back everything one the first line and the second line with some added content.

s_^\([ </p>]*<h2 id="\)\(b0[0-9][0-9]c[0-1][0-9][0-9]\)\(" class="chapter">[0-9]\+</h
2><p>\)\n\([ ]*\)\([A-Z]\)_\1\2\3\r\4<span id="\2v001" class="verse">1</span> \5_
Does anyone know how to do this with sed or something else on the shell cli?
Old 12-09-2012, 11:03 PM   #2
Registered: Nov 2005
Distribution: Slackware, Arch
Posts: 43

Rep: Reputation: 8
Give us an example of the two consecutive lines _and_ an example of the result you want to accomplish.
Old 12-09-2012, 11:30 PM   #3
Registered: Sep 2004
Distribution: PCLinuxOS
Posts: 59

Original Poster
Rep: Reputation: 15
What I have
<h2 id="b001c001" class="chapter">1</h2><p>

What I want
<h2 id="b001c001" class="chapter">1</h2><p>
<span id="b001c001v001" class="verse">1</span> I

The second line has more than just the one capital letter but the rest of the line isn't touched or changed. Like I said in my OP this works all day long in Vim very well. I would rather run it from the cli doing about 1200 pairs of lines within several dozen files.
Old 12-10-2012, 04:25 AM   #4
LQ Guru
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,786

Rep: Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888
here is a tutorial about multi line regex and sed:
otherwise perl is suggested, probably it handles this case better

this will do something similar (what you wish), but I'm not really sure:
perl -we '
undef $/;
while ( <> ) { s!
               ^([ ]*</p>)?                             # $1
	       (<h2[ ]id=")                             # $2
	       (b0[0-9][0-9]c[0-1][0-9][0-9])           # $3
	       ("[ ]class="chapter">[0-9]+</h2><p>)     # $4
	       \n                                       # $
	       ([ ]*)                                   # $5
	       ([A-Z])                                  # $6
              !$2$3$4\n$5<span id="$3v001" class="verse">1</span> $6
	    }' inputfile


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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
sed command search and replace zulkifal Linux - Newbie 8 11-26-2012 11:56 AM
Using sed - search and replace seebee Programming 5 06-07-2011 10:47 PM
[SOLVED] Using sed to search and replace backwards jimieee Programming 15 10-25-2010 11:13 AM
sed search replace tomerbd1 Linux - General 9 04-10-2008 05:31 AM
sed question for search and replace jakev383 Linux - General 8 05-05-2007 06:40 AM > Forums > Linux Forums > Linux - General

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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration