每一场比赛后搜索一次

问题描述 投票:-1回答:2

我正在尝试创建一个脚本来获取特定单词“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行像

=失败

要么

=无法=无法

linux string perl search
2个回答
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


2
投票

-l具有在每个print之后打印换行的效果。

以下将打印请求的URL的(第一个)statusDescription字段的值(忽略引用者URL)。它甚至会为你正确解码值(如果它包含转义字符,如+%20)。

perl -MURI -MURI::QueryParam -nle'
   my ($request_url) = /^\S+\s+(\S+)/
      or next;
   $request_url = URI->new($request_url, "http");
   my $status = $request_url->query_param("statusDescription")
      or next;
   print $status;
' test.txt
© www.soinside.com 2019 - 2024. All rights reserved.