From 9a95a8add6a4d56016b56c2ecb3622baa12b34c1 Mon Sep 17 00:00:00 2001 From: Vincent Schweiger Date: Sun, 28 Jan 2024 15:34:35 +0100 Subject: [PATCH] add simple command line opts --- filter.pl | 44 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/filter.pl b/filter.pl index 5f6c40e..d2ebfaa 100755 --- a/filter.pl +++ b/filter.pl @@ -1,22 +1,50 @@ #!/usr/bin/perl use strict; use warnings; +use Getopt::Long; + +my $newlines = ''; +my $file = ''; +GetOptions ("newlines" => \$newlines, # print newlines + "file=s" => \$file # input file +) or die("Error in command line arguments\n"); my %seen = (); -for (;;) { - my $input = ; - last if not defined $input; - chomp $input; - my @l = $input =~ /([0-9]+\/[0-9]+\/[0-9]+)/mg; - foreach my $item (@l) { - $seen{$item}++; +my $in_file; + +if ($file) { + open my $in_file, $file or die "Could not open $file: $!"; + while (my $line = <$in_file>) { + last if not defined $line; + chomp $line; + my @l = $line =~ /([0-9]+\/[0-9]+\/[0-9]+)/mg; + foreach my $item (@l) { + $seen{$item}++; + } + } + close $in_file; +} else { + for (;;) { + my $input = ; + last if not defined $input; + chomp $input; + my @l = $input =~ /([0-9]+\/[0-9]+\/[0-9]+)/mg; + foreach my $item (@l) { + $seen{$item}++; + } } } my @uniq = sort gw_comp keys %seen; if (@uniq) { - print "@uniq\n"; + if ($newlines) { + foreach my $item (@uniq) { + print "$item\n"; + } + } else { + print "@uniq\n"; + } } sub gw_comp{