Multi-line regular expression matching
Brad De Vries
devriesbj
Mon May 17 11:48:03 PDT 2004
--- listmail at rotundus.com wrote:
> I'm attempting to write a script that will delete a
> certain portion of a
> data file. What I want to do is delete everything
> between (and including)
> two lines, line A and line B
>
> The "file":
> ...
> #lineA
> ...
> #lineB
> ...
>
> To an extent I don't really care what tool is used,
> but I've been unable
> to accomplish the task with either Perl or SED so
> I'm wondering if there
> might be something wrong with the regular
> expressions that I've tried.
>
> A couple of the reg-exp's that I've tried:
> 's/#lineA.*#lineB//gs'
> 's/#lineA(.|\n)*#lineB//gs'
>
> Both of the above seem to work fine if #lineA and
> #lineB are on the same
> line in the file (I've yet to add in anything to
> check if #lineA and
> #lineB occur at the start of the line, but that'll
> be added in later).
> Shouldn't the /s modifier allow multi-line matching
> to take place?
>
> David Aikema
Here's a little 3-line awk script that should work.
----
# set flag when we find the beginning
/lineA/ { found = 1 }
# print only when we haven't found the beginning
{ if (! found) print($0) }
# clear flag when we find the end
/lineB/ { found = 0 }
----
Here's another example, this time using vim.
command:
vi -s script-file data-file
script-file contains:
/lineA
d/lineB
dd
:x data-file.new
----
HTH,
Brad.
__________________________________
Do you Yahoo!?
Yahoo! Calendar - Free online calendar with sync to Outlook(TM).
http://calendar.yahoo.com
More information about the Linux-users
mailing list