我正在尝试创建一个脚本来获取特定单词“ statusDescription”之后的所有Apache响应,但是我遇到的问题是,某些行的输出重复了,因为匹配的单词或响应可能在同一行写了2次
日志“ 1行”的样本:GET/en?status=1&newMainBalance=5486&serviceAmount=700&ExternalTrxId=asdf&PgTrxId=tfpsadf&amount=0&statusDescription=Failed&customerCode=1.1&newDedicatedBalance=0&secureHash=56a7sdyf&paidAmount=1000&responseMsg=%a1%a1%A1(PG_ID)&language=enHTTP/1.1"200186243**1/1210669**1"-""-""-""https://example.com.eg?statusDescription=Failed&externalTrxId=123&status=203&secureHash=asdf&pgTrxId=asdf
我尝试使用以下命令获取“ statusDescription”和“&”之间的任何匹配项
cat test.txt | perl -nle'print $1 while /statusDescription(.*?)\&/g'
cat test.txt | perl -nle'print $1 while /statusDescription(.*?)\&/g'
输出:
=失败
=失败
我除了结果是1行只喜欢
=失败
要么
=失败=失败
最佳答案
您使用的-l
选项没有值。引用perlrun:
如果省略octnum,则将$\
设置为$/
的当前值
默认情况下为\n
。因此,每张印刷品都会自动添加\n
。
$ echo 'Match=once& should only Match=twice& once' | perl -nle 'print "MATCH ->$1<-" while /Match(.*?)&/g'
MATCH ->=once<-
MATCH ->=twice<-
# the same without -l
$ echo 'Match=once& should only Match=twice& once' | perl -ne 'print "MATCH ->$1<-" while /Match(.*?)&/g'
MATCH ->=once<-MATCH ->=twice<-
# if your post-processing requires matches from different lines to appear
# on separate lines then append a conditional print "\n" at the end
$ echo 'Match=once& should only Match=twice& once' | perl -ne 'print "MATCH ->$1<-" while /Match(.*?)&/g; print "\n" if $1'
MATCH ->=once<-MATCH ->=twice<-
$ echo 'nomatch=once& should only nomatch=twice& once' | perl -ne 'print "MATCH ->$1<-" while /Match(.*?)&/g; print "\n" if defined $1'
$
对于
$/
(输入记录分隔符)和$\
(输出记录分隔符),另请参见perlvar。