Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Can awk, sed match mult-line patterns ?
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.


Report this thread to moderator Post Follow-up to this message
Old Post
problems@gmail
03-20-08 11:59 PM


Re: Can awk, sed match mult-line patterns ?
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.

Report this thread to moderator Post Follow-up to this message
Old Post
A Watcher
03-20-08 11:59 PM


Re: Can awk, sed match mult-line patterns ?
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)



Report this thread to moderator Post Follow-up to this message
Old Post
Ted Davis
03-20-08 11:59 PM


Re: Can awk, sed match mult-line patterns ?
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

Report this thread to moderator Post Follow-up to this message
Old Post
notbob
03-20-08 11:59 PM


Re: Can awk, sed match mult-line patterns ?

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.



Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
03-20-08 11:59 PM


Re: Can awk, sed match mult-line patterns ?
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.
>

Report this thread to moderator Post Follow-up to this message
Old Post
Rajan
03-24-08 03:00 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 03:27 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.