什么是Perl的相当于awk的/文本/,/ END /? [英] What is Perl's equivalent to awk's /text/,/END/?
问题描述
我期待取代使用awk来修剪了一些HTML讨厌的shell脚本。问题是我无法找到在Perl什么,做上述功能
的awk'/<表\\宽\\ = \\100 \\%\\BORDER \\ = 1 \\ CELLSPACING \\ = 0>< TR \\类\\ = \\tabhead \\ ><第i个国家和LT; \\ /第i /,/ END /'
我怎么能在Perl中做到这一点?
预期产出将是
< TABLE WIDTH =100%BORDER = 1 CELLSPACING = 0>< TR类=tabhead><第i个国家和LT; / TH>
Perl的触发器运营商给了我更多的路。 (星号的任何东西都是垃圾)
*< H2>浏览监视器(1淘汰497)LT; / H>< BR><字体大小= - 1的风格=FONT-重量:正常的>使用的< A HREF = /的SiteScope / CGI / go.exe / SiteScope的页= monitorSummary&放大器;占= login15>监控说明报告和LT; / A>查看当前的监视器配置< / FONT> *< TABLE WIDTH =100%BORDER = 1 CELLSPACING = 0>< TR类=tabhead><第i个国家和LT; / TH>
我认为这将工作:
的perl -ne'如果打印/文本/ .. / END /'
直到遇到一个行,其中表达式1
是真实的
表达式1 ..表达式2
将是错误的。
然后,直到遇到一个行,其中表达式2
是真的,那将是真实的。
更新:如果您需要修剪从第一个匹配行前面的非匹配的文本,这将工作
的perl -ne'打印如果s /.* TEXT / TEXT / .. S / END * / END /`
或
的perl -ne'打印如果s /.*(文本)/ $ 1 / .. S /(END)。* / $ 1 /'
如果文本是一个很长的字符串,你只需要输入一次。而它的模式匹配的变化将编辑该行。
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.
这篇关于什么是Perl的相当于awk的/文本/,/ END /?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!