Friday, May 27, 2011

How to Remove Lines Starting With a Specific Word in Perl

Method 1: The Perl One-Liner (Fastest for Command Line)
This is the quickest way to remove specific lines directly from the terminal without writing a full script.

Bash
# -p loops through the file and prints each line
# -i edits the file in-place
# The regex substitutes any line starting with 'HPL_' with nothing.
perl -pi -e 's/^HPL_.*//s' myfile.txt

Method 2: Reading and Writing Line-by-Line (Memory Efficient)
This method reads the input file one line at a time, checking the condition before writing to the output file. This is ideal for very large files.

Perl
#!/usr/bin/perl
use strict;
use autodie;
use warnings FATAL => "all";

my $infile = "myfile.txt";
my $outfile = "changed.txt";

open( my $infh, '<', $infile );
open( my $outfh, '>', $outfile );

# Process the file line by line
while( my $line = <$infh> ) {
   # Skip to the next line if the current one starts with 'HPL_'
   next if $line =~ /^HPL_/;
   
   # Otherwise, print the line to the output file
   print $outfh $line;
}

close( $outfh );
close( $infh );

Method 3: Slurping and Filtering with grep (Best for Small/Medium Files)
This method reads the entire file into an array first, filters it using grep, and then writes the result to a new file.

Perl
#!/usr/bin/perl
use strict;
use warnings;

open(my $in, '<', 'myfile.txt') or die "failed to open input for read: $!";
# Read all lines into an array
my @lines = <$in> or die 'no lines to read from input';
close($in);

# Collect all lines that do NOT (!) begin with HPL_ into @result
my @result = grep ! /^HPL_/, @lines;

open(my $out, '>', 'changed.txt') or die "failed to open output for write: $!";
# Print the filtered array to the new file
print { $out } @result;
close($out);


No comments: