Can awk, sed match mult-line patterns ?

  • Follow


It seems to me that sed & awk are single line based ?

I want to delete blocks of text which look like:
<opening pattern>
<2 to 5 lines>
<closing pattern>

If I use the <2 to 5 lines>, I can avoid deleting all the good
stuff, up to the 'next' <closing pattern> in case the assumed
<closing pattern> is missing/undetected.

Thanks for any feedback,

== Chris Glur.

0
Reply problems 3/20/2008 3:55:05 PM

On Mar 20, 7:55 am, problems@gmail wrote:
> It seems to me that sed & awk are single line based ?
>
> I want to delete blocks of text which look like:
> <opening pattern>
> <2 to 5 lines>
> <closing pattern>
>
> If I use the <2 to 5 lines>, I can avoid deleting all the good
> stuff, up to the 'next' <closing pattern> in case the assumed
> <closing pattern> is missing/undetected.
>
> Thanks for any feedback,
>
> == Chris Glur.

ex is well suited for this kind of edit.  ex is the command line part
of vi (what you type at the colon).   I'm busy right now but if you
google
ex edit tutorial
you should see how to use it.
0
Reply A 3/20/2008 4:21:32 PM


On Thu, 20 Mar 2008 10:55:05 -0500, problems wrote:

> It seems to me that sed & awk are single line based ?
> 
> I want to delete blocks of text which look like: <opening pattern>
> <2 to 5 lines>
> <closing pattern>
> 
> If I use the <2 to 5 lines>, I can avoid deleting all the good stuff, up
> to the 'next' <closing pattern> in case the assumed <closing pattern> is
> missing/undetected.
> 
> Thanks for any feedback,
>

Pseudocode:

	if line matches opening pattern, set flag
	if flag is clear, print line
	if line matches closing pattern, clear flag

If you want to keep the two pattern lines, interchange the locations of
the two tests.

-- 
T.E.D. (tdavis@mst.edu)


0
Reply Ted 3/20/2008 7:55:29 PM

On 2008-03-20, problems@gmail <problems@gmail> wrote:

> I want to delete blocks of text which look like:
><opening pattern>
><2 to 5 lines>
><closing pattern>

> Thanks for any feedback,

Try these for ideas.  See the "SELECTIVE DELETION OF CERTAIN LINES:"
section.

http://www.eng.cam.ac.uk/help/tpl/unix/sed.html

nb
0
Reply notbob 3/20/2008 8:12:01 PM


On 3/20/2008 10:55 AM, problems@gmail wrote:
> It seems to me that sed & awk are single line based ?

sed is line-based, awk is record-based.

> I want to delete blocks of text which look like:
> <opening pattern>
> <2 to 5 lines>
> <closing pattern>

Use:

	awk -v RS="<closing pattern>" '...' file

to use "<closing pattern>" as the record separator.

> If I use the <2 to 5 lines>, I can avoid deleting all the good
> stuff, up to the 'next' <closing pattern> in case the assumed
> <closing pattern> is missing/undetected.

Post some sample input and expected output for more help.

	Ed.


0
Reply Ed 3/20/2008 10:26:02 PM

awk can definitely understand multi-line patterns. All that you have to do 
is set the RS (record separator) variable. The best place to do this would 
be in the BEGIN however you can change this anytime during the execution. 
The best part is RS can also be a regular expression like RS="</[^>]*>" .

<problems@gmail> wrote in message news:1206028330.11645@vasbyt.isdsl.net...
> It seems to me that sed & awk are single line based ?
>
> I want to delete blocks of text which look like:
> <opening pattern>
> <2 to 5 lines>
> <closing pattern>
>
> If I use the <2 to 5 lines>, I can avoid deleting all the good
> stuff, up to the 'next' <closing pattern> in case the assumed
> <closing pattern> is missing/undetected.
>
> Thanks for any feedback,
>
> == Chris Glur.
> 
0
Reply Rajan 3/23/2008 11:28:19 PM

5 Replies
382 Views

(page loaded in 0.065 seconds)

Similiar Articles:













7/20/2012 9:49:39 PM


Reply: