我是Perl的新手,所以请容忍我的简单问题:
以下是示例输出:
Most successful agents in the Emarket climate are (in order of success):
1. agent10896761 ($-8008)
2. flightsandroomsonly ($-10102)
3. agent10479475hv ($-10663)
Most successful agents in the Emarket climate are (in order of success):
1. agent10896761 ($-7142)
2. agent10479475hv ($-8982)
3. flightsandroomsonly ($-9124)
我只对代理名称以及它们对应的余额感兴趣,所以我希望得到以下输出:
agent10896761 -8008
flightsandroomsonly -10102
agent10479475hv -10663
agent10896761 -7142
agent10479475hv -8982
flightsandroomsonly -9124
用于以后的流程。
这是我到目前为止得到的代码:
#!/usr/bin/perl -w
open(MYINPUTFILE, $ARGV[0]);
while(<MYINPUTFILE>)
{
my($line) = $_;
chomp($line);
# regex match test
if($line =~ m/agent10479475/)
{
if($line =~ m/($-[0-9]+)/)
{
print "$1\n";
}
}
if($line =~ m/flightsandroomsonly/)
{
print "$line\n";
}
}
第二个正则表达式匹配没有任何错误,因为它会打印出整行内容。
$ ./compareResults.pl 3.txt
2. flightsandroomsonly ($-10102)
0479475
0479475
3. flightsandroomsonly ($-9124)
1. flightsandroomsonly ($-8053)
0479475
1. flightsandroomsonly ($-6126)
0479475
如果我像这样“转义”花括号
if($line =~ m/\($-[0-9]+\)/)
{
print "$1\n";
}
那么第一个正则表达式永远不会匹配...
因此,我遇到了一个问题,那就是如何让特定的正则表达式工作。对此有什么提示吗?在此之前,非常感谢您。
发布于 2010-04-08 22:52:13
perl -ane '$F[2]=~s/\(|\)//g;print "$F[1] $F[2]\n" if $F[1]=~/agent|flight/' file
发布于 2010-04-08 23:30:37
use strict;
use warnings;
while(<DATA>){
#split on whitespaces, pick 2nd and 3rd items
#check 2nd item matches pattern, do some trimming to 3rd
#store them to @data and print them
my @data =grep{/\w{13,}/ || s/\(\$|\)//g;}((split' ')[1,2]);
print join("\t",@data),"\n" if (@data);
}
__DATA__
1. agent10896761 ($-8008)
2. flightsandroomsonly ($-10102)
3. agent10479475hv ($-10663)
Most successful agents in the Emarket climate are (in order of success):
1. agent10896761 ($-7142)
2. agent10479475hv ($-8982)
3. flightsandroomsonly ($-9124)
__OUTPUT__
agent10896761 -8008
flightsandroomsonly -10102
agent10479475hv -10663
agent10896761 -7142
agent10479475hv -8982
flightsandroomsonly -9124
https://stackoverflow.com/questions/2600844
复制相似问题