LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 12-09-2012, 10:26 PM   #1
the_rhino
Member
 
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.

Code:
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
steelneck
Member
 
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
the_rhino
Member
 
Registered: Sep 2004
Distribution: PCLinuxOS
Posts: 59

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

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
pan64
Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 5,018

Rep: Reputation: 1324Reputation: 1324Reputation: 1324Reputation: 1324Reputation: 1324Reputation: 1324Reputation: 1324Reputation: 1324Reputation: 1324Reputation: 1324
here is a tutorial about multi line regex and sed: http://www.grymoire.com/Unix/Sed.html#uh-51
otherwise perl is suggested, probably it handles this case better

this will do something similar (what you wish), but I'm not really sure:
Code:
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
	      !msx;
	      print;
	    }' inputfile
 
  


Reply


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


All times are GMT -5. The time now is 10:19 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