in reply to Regex to undo a regex?
If I understand the problem correctly you are trying to eliminate duplicate lines that match a pattern where the lines immediately follow each other.
This code will open a file called file.html and if there are two or more lines in succession that are identical and contain the text sample then the duplicates are removed and file.html is rewritten.
use IO::String; my $regex = qr/sample/; my $file = 'file.html'; open my $in , '<', $file or die "Can't open $file: $!\n"; my $new_html; my $new = IO::String->new($new_html); my $last = ''; my $modified; while ( my $line = <$in> ) { if ( $line =~ /$regex/ ) { if ($line ne $last) { print {$new} $line; } else { ++$modified; } } else { print {$new} $line; } $last = $line; } close $in; close $new; if ($modified) { rename $file, "$file.bak" or die "Can't rename $file: $!\n"; open my $out, '>', $file or die "Can't open $file: $!\n."; print {$out} $new_html or die "Can't print to $file: $!\n." }
Update: Just read shmem's rather neat answer and updated my code to make a backup if the file is modified.
|
|---|