Skip to content

Instantly share code, notes, and snippets.

@andreburto
Created February 8, 2018 02:45
Show Gist options
  • Save andreburto/8cd4455ed50403a4210b9c4f86b82924 to your computer and use it in GitHub Desktop.
Save andreburto/8cd4455ed50403a4210b9c4f86b82924 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl -w
######################################################################
# skip_lines.pl -- skips selected lines of a given file
#
# perl skip_lines.pl /path/to/file.txt 1,3,4,5
# | | |
# script -----+ | |
# text file ---------------+ |
# comma separated line numbers ---------+
# with no spaces
######################################################################
use strict;
use warnings;
my ($file, $skip_lines) = @ARGV;
my $sl_cnt = 0;
my $rl_cnt = 0;
my @skip_lines = split(/,/, $skip_lines);
open(my $IN, $file) || die "Could not open file.";
LINE: while (<$IN>) {
$rl_cnt++;
foreach my $sl (@skip_lines) { if ($sl == $rl_cnt) { next LINE; } }
print $_;
}
close($IN);
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment