Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

awk

perl

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>
like image 328
kSiR Avatar asked Mar 26 '10 19:03

kSiR


People also ask

What does !~ Mean in Perl?

!~ is the negation of the binding operator =~ , like != is the negation of the operator == . The expression $foo !~ /bar/ is equivalent, but more concise, and sometimes more expressive, than the expression !($foo =~ /bar/)

WHAT IS A in awk?

a is nothing but an array, in your code FNR==NR{ a[$1]=$0;next } Creates an array called "a" with indexes taken from the first column of the first input file. All element values are set to the current record. The next statement forces awk to immediately stop processing the current record and go on to the next record.


1 Answers

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.

like image 153
mob Avatar answered Sep 20 '22 23:09

mob