#!/usr/bin/perl use strict; use warnings; my @GREPLIST = grep {!/^[\+\-\~]/} @ARGV; my @SKIPLIST = grep {s/^~//} @ARGV; my $DEBUG = grep {/^-d/} @ARGV; my $IGNORECASE = grep {/^-i/} @ARGV; (my @POSITIONS) = grep {s/^\+//} @ARGV; # add this to regexp with ignore case if($IGNORECASE){ grep { s/^/(?i)/ } @GREPLIST; } print "# POSITIONS=@POSITIONS;IGNORECASE=$IGNORECASE;GREPLIST=@GREPLIST;SKIPLIST=@SKIPLIST\n" if $DEBUG; # arrays start with zero, so decrease field numbers by 1 --$_ for @POSITIONS; LINE: while(){ for my $re (@GREPLIST){ print "# $re =~ $_" if ($DEBUG); next LINE unless m/$re/; } for my $re (@SKIPLIST){ print "# $re !~ $_" if ($DEBUG); next LINE if m/$re/; } chomp; if(@POSITIONS){ # print all fields in the requested order print join(" ", +(split /\s+/)[@POSITIONS]); }else{ # just print the whole line print } print "\n"; } #### cat ~/a | ./gggrep.pl country city1 street1 ~name1 -i +5 name2 name3 cat ~/a | ./gggrep.pl -i country city1 street1 ~name1 +2 +5 City1 name2 City1 name3