我需要在PgTrx
之后得到一个单词,在Statusdescription
之后得到一个单词
日志示例:
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
之后我只得到1个字:
perl -ne 'foreach my $p (split(/&/)) { if ($p =~ /statusDescription(=.*)/) { print "$1\n"; last } }'
cat file.txt | perl -ne 'forea ch my $p (split(/&/)) { if ($p =~ /statusDescription(=.*)/) { print "$1\n"; last } }'
我想得到如下结果。
PgTrxId=tfpsadf&statusDescription=Failed
但我只有
statusDescription=Failed
答案 0 :(得分:3)
不要四处闲逛。将整行解析为哈希,然后选择所需的键:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
my @wanted = qw ( PgTrxId statusDescription );
while (<DATA>) {
my %param = m/(\w+)=(\w+)/g;
print Dumper \%param;
print join ( "&", map { "$_:$param{$_}" } @wanted ),"\n";
}
__DATA__
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
如果您希望将其作为一个衬纸:
perl -ne '%p = m/(\w+)=(\w+)/g; print join ( "&", map { "$_=$p{$_}" } ) qw ( PgTrxId statusDescription ),"\n"'
为了简洁起见,我在这里使用了map
,因为它在一个衬里中很有用,但是在功能上等同于:
my @list_of_wanted_things;
foreach my $word ( @wanted ) {
push ( @list_of_wanted_things, "$word=$param{$word}" );
}
print join "&", @list_of_wanted_things;
答案 1 :(得分:1)
您要更改的两个明显的事情:
last
,以便在找到(?: ... )
之后不会停止。我还使用了perl -ne 'foreach my $p (split(/&/)) { if ($p =~ /(?:statusDescription|PgTrxId)(=.*)/) { print "$1\n" } }'
来聚集搜索词而不捕获它们。
for row in file_data:
if row['Country']=='India':
country.append(row['Country'])
killed.append(int(row['Killed']))
wounded.append(int(row['Wounded']))
city.append(row['City'])
final.append([row['City'],row['Killed'],row['Wounded']])
但我更喜欢Sobrique's solution。
答案 2 :(得分:0)
如果您的数据在“ d”文件中,
perl -ne '@s = /\b(PgTrxId=[^&]+)|(&statusDescription=[^&]+)/xg;print @s' d
结果在数组变量@s
中