开发者

What is Perl's equivalent to awk's /text/,/END/?

开发者 https://www.devze.com 2022-12-24 14:59 出处:网络
I am looking to replace a nasty shell script that uses awk to trim down some HTML. The problem is I cannot find anything in Perl that does the aforementioned function

I am looking to replace a nasty shell script that uses awk to trim down some HTML. The problem is I cannot find anything in Perl that does the aforementioned function

awk '/<TABLE\ WIDTH\=\"100\%\" BORDER\=1\ CELLSPACING\=0><TR\ class\=\"tabhead\"><TH>State<\/TH>/,/END/'

How can I do this in Perl?

the expected output would be

<TABLE WIDTH="100%" BORDER=1 CELLSPACING=0><TR class="tabhead"><TH>State</TH>

The Perl flipflop operator gives me WAY more. (Everything between the asterisks is junk)

*<h2>Browse Monitors (1开发者_如何转开发 out of 497)</h2><br><font size="-1" style="font-weight:normal"> Use the <A HREF=/SiteScope/cgi/go.exe/SiteScope?page=monitorSummary&account=login15 >Monitor Description Report</a> to view current monitor configuration settings.</font>*<TABLE WIDTH="100%" BORDER=1 CELLSPACING=0><TR class="tabhead"><TH>State</TH>


I think this will work:

perl -ne 'print if /text/ .. /END/'

expr1 .. expr2 will be false until it encounters a line where expr1 is true. Then it will be true until it encounters a line where expr2 is true.


Update: if you need to trim the non-matching text from the front of the first matching line, this will work

perl -ne 'print if s/.*TEXT/TEXT/ .. s/END.*/END/`

or

perl -ne 'print if s/.*(TEXT)/$1/ .. s/(END).*/$1/'

if TEXT is a long string that you only want to type once. The change will edit the line while it does the pattern match.


As a one-liner (slightly changed since first post):

perl -n -e '$started = 1 if /<TABLE\ WIDTH\=\"100\%\" BORDER\=1\ CELLSPACING\=0><TR\ class\=\"tabhead\"><TH>State<\/TH>/; next unless $started; print; last if /END/;'

From the perlrun man page:

   -n   causes Perl to assume the following loop around your program,

which makes it iterate over filename arguments somewhat like sed -n or awk:

          LINE:
            while (<>) {
                ...             # your program goes here
            }

And then the core of the body is to wait for the start, then print every line until the end.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号