Perl command line one-liner to find regular expression matches that span multiple lines?
For example, I have a file foo.txt
that contains
abc
bcc
ccc
baa
and I want to return the indexes of all instances of "c\nb" (in this case, the string is found at starting at the third and eleventh characters of the file). What's the simplest way t开发者_JS百科o accomplish this?
If you're sure the file is small enough to fit comfortably into memory, you can just slurp it into a variable and apply a regex to it:
$ perl -0777 -ne 'print $-[0], "\n" while /c\nb/g' foo.txt
Otherwise:
$ perl -ne 'print $n - 2, "\n" if /^b/ && $last =~ /c$/; $last = $_; $n += length' foo.txt
Note that these solutions depend on the input file containing only ASCII characters.
#!/usr/bin/perl
my $s = 'abc
bcc
ccc
baa';
while ($s =~ /c\nb/mg ) {
print pos($s), "\n"
}
this will output 5, 13 (the pos
function returns the index of the end of the match, but you should be able to compensate for that).
I'm sure there's a decent regex solution to this, but I'll fall back on the older index
function:
$_ = q[abc
bcc
ccc
baa];
my $z; print $z++,"\n" while 0<=($z=index($_,"c\nb",$z));
2
10
精彩评论