Re: Regex and writing lines in file
by Corion (Patriarch) on Apr 30, 2013 at 11:19 UTC
|
If your three lines are not in the string buffer you are matching against already, you cannot solve this with regular expressions. I would maintain a "skip" count to know how many lines to skip:
my $skip;
while (<>) {
next
if $skip--;
if( /mypattern/ ) {
$skip= 3;
} else {
print "Have line '$_'";
};
};
If your three lines are in the string buffer already, a new line is defined by the newline character(s) appearing. So you would match three newline charachters with non-newline characters in between them to skip three lines forward.
Without seeing code, it's hard to advise better. | [reply] [d/l] |
Re: Regex and writing lines in file
by choroba (Cardinal) on Apr 30, 2013 at 11:24 UTC
|
Perl regular expressions are powerful. This kind of activity would be considered obfuscation, though, and I would not recommend it for production code:
perl -pe 's/.*pattern\n$/<>for 1..3;$_/e'
| [reply] [d/l] |
Re: Regex and writing lines in file
by hdb (Monsignor) on Apr 30, 2013 at 11:27 UTC
|
use strict;
use warnings;
open my $fh, "<", "somefile.txt";
while(my $line = <$fh>){
if($line =~ /pattern/){
print "Matched: $line";
print $line if defined( $line = <$fh>);
print $line if defined( $line = <$fh>);
}
}
close($fh);
| [reply] [d/l] |
Re: Regex and writing lines in file
by Anonymous Monk on Apr 30, 2013 at 11:17 UTC
|
you can't, regex doesn't do file i/oread perlintro | [reply] |
Re: Regex and writing lines in file
by Random_Walk (Prior) on Apr 30, 2013 at 13:54 UTC
|
#!/usr/bin/perl
use strict; use warnings;
my $file;
my $match = "that";
my $extra = "Hello, I snook in here\nme too!";
{
local $/;
$file = <DATA>;
$file =~ s/$match\n([^\n]*)\n([^\n]*)/$match\n$1\n$2\n$extra/;
}
print $file;
__DATA__
this
that
leave me
leave me
move me
and me
Output
this
that
leave me
leave me
Hello, I snook in here
me too!
move me
and me
Cheers, R.
Pereant, qui ante nos nostra dixerunt!
| [reply] [d/l] [select] |
|
$extra = "This is i will place after match\n.an this too";
open (my $fh, '+<', $file);
$data = <$fh>;
$data = s/(.*entry)\n([^\n]*)\n/$1\n$extra/
The $1 is found successfully. But could not do the substitution in the file.
| [reply] [d/l] |
|
# untested
seek $fh, 0, 0; # rewind to the start of the file
print $fh $data; # Write the entire file
close $fh;
Of course this may not be very efficient if you have a large file to alter, but then inserting into the middle of a very big file never is. If you are frequently updating random records in a file you may need to start thinking about using a database.
Cheers, R.
Pereant, qui ante nos nostra dixerunt!
| [reply] [d/l] [select] |
|
Re: Regex and writing lines in file
by BillKSmith (Monsignor) on Apr 30, 2013 at 14:59 UTC
|
Do we have to test for matches in the three lines after the first match?
| [reply] |
|
| [reply] |
|
The following code uses an array as a "circular list" to delay the printing of the EXTRA. Is this what you need?
use strict;
use warnings;
my $match = qr /Match/;
my $extra = qq(EXTRA\nEXTRA\n);
my @extras = (q()) x 3;
my $in = 3;
my $out = 0;
while (my $line = <DATA>) {
$out = ($out+1) % 4;
print $line, $extras[$out];
$in = ($in +1) % 4;
$extras[$in] = ($line =~ /$match/)? $extra : q();
}
__DATA__
a
b
Match1
Match2
c
d
e
f
g
Match3
h
i
j
k
| [reply] [d/l] |