Reputation: 59
How to parse only a line which has a word QUERY? I tried:
my @strs = loadf('ck.txt');
while(1)
{
my $str = shift @strs;
if(!$str)
{
exit;
}
if($str =~ /QUERY/)
{
($cl) = $str =~ /QUERY: (.*?)/;
open F, ">>go.txt";
print F $cl;
close F;
}
}
sub loadf {
open (F, "<".$_[0]) or erroropen($_[0]);
chomp(my @data = <F>);
close F;
return @data;
}
ck.txt:
22.11.2012 16:55:45 +02:00
IP: 99.992.92.992
QUERY: BANNER_LANG=ru; textext community-lang=ru
REFERER: http:/site.ru/827
AGENT: Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.10
22.11.2012 16:55:44 +02:00
IP: 89.189.191.6
QUERY: BANNER_LANG=ru; text; community-lang=ru
REFERER: http:/site.ru/444
AGENT: Opera/9.80 (Windows NT 5.1; U; ru) Presto/2.10.289 Version/12.00
But it doesn't work=\
Upvotes: 1
Views: 89
Reputation: 67900
You are complicating things. Your problem can be solved with a one-liner:
perl -nlwe 'print if /^QUERY/' query.txt >> go.txt
You may consider removing the start of line anchor ^
from the regex if your data is irregular.
If you wish to remove the word QUERY:
you can use this one-liner:
perl -nlwe 'print if s/^QUERY:\s*//' query.txt >> go.txt
The deparsed code for this one-liner is (edited for simplicity):
use warnings; # -w option: enable warnings
BEGIN { $/ = "\n"; $\ = "\n"; } # -l option: handle line endings
while (<>) { # -n option: read input file or stdin
chomp; # -l option again
print $_ if s/^QUERY:\s+//; # our code
}
Upvotes: 0
Reputation: 6566
Your problem was the (.*?)
. ?
makes the match non-greedy, so it will match as few characters as possible while still satisfying the regex. In this case, that was always zero characters.
Also, your code can be simplified:
use strict;
use warnings;
my @strs = ('BLAH', ' QUERY: foobarbaz', 'QUERY asdf');
#open the file once: more efficient.
open my $file, '>>', 'go.txt' or die "Can't open file: $!";
for my $str (@strs)
{
#Perform all matching logic in one go.
if($str =~ m/QUERY: (.*)/)
{
print {$file} $1;
}
}
close $file;
Upvotes: 1
Reputation: 2852
Either remove the ?
to make it greedy or add $
after the closing )
Eg
QUERY: (.*)
or
QUERY: (.*?)$
Upvotes: 0