grep -v equivalent in perl

I have to do grep -v in a perl script. I want to exclude blank lines and lines having visitor.

#grep -v visitor abc.txt |grep '.'
 
 
file:abc.txt
1340 not booked 16D:D9 tourist 8 
1341 not booked 16C:D4 tourist 25
1342 not booked 16D:C4 visitor 7 
1343 not booked 01C:D9 visitor 6 
1344 not booked 01D:C9 visitor 43
1345 not booked 16C:C9 visitor 7 
1346 not booked 01D:D4 visitor 8 
1347 not booked 01C:C4 visitor 90
1348 not booked 16D:D5 tourist 32
1349 not booked 16C:D6 tourist 23

You can execute the same command within perl also

#!/bin/perl
my $var=`grep -v visitor abc.txt |grep '.'`;
print $var;

regards,
Ahamed

1 Like

Hi,

Try next command:

$ perl -e '@a = <>; print +(grep { ! /^(?:\s*)$|(?i:visitor)/ } @a)' infile

Regards,
Birei

Will be better is you show us your script , anyway :

 perl -n -e 'if (!/visitor|^$/){print;}' file

An alternative (and nice) perl idiom:

perl -n -e 'print unless /visitor|^$/;' file

Thank so much ahamed101, birei and klashxx. ahamed101 script is very useful.