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

I want to replace a nasty shell script that uses awk to trim some HTML. The problem is that I cannot find anything in Perl, which performs the above function

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

How can I do this in Perl?

expected result will be

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

The flipflop Perl statement gives me more options. (Everything between the stars is trash)

 *<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> 
+4
source share
2 answers

I think this will work:

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

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


Update: if you need to trim inconsistent 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 want to enter only once. The change will change the line as long as it matches the pattern.

+5
source

As a single line (slightly modified from the 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/;' 

On the man perlrun page:

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

which makes it iterate over the file name the arguments are somewhat similar to sed -n or awk:

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

And then the core of the body should wait for the beginning, and then print each line to the end.

0
source

Source: https://habr.com/ru/post/1305249/


All Articles