Phil for Humanity Phil for Humanity
A Guide for the Survival of Humankind and Helping the World, Society, and Yourself.



How to Implement "grep v" in Perl



The UNIX grep command has a very useful argument, simply known as "-v", that can find all lines unless it has the following argument anywhere in each line. Here is an example of using this command:

grep -v STRING FILENAME

So, if you had a file with these lines as its contents.

ABCDEF
GHI DOG JKL
MNOP DG QR
DOG ST UV
XYZ

And you run this "grep -v DOG filename.txt" command, then the output would be this:

ABCDEF
MNOP DG QR
XYZ

Well, you can do the same command in Perl with this single line of code:

grep { $_ !~ /STRING/i } @RAY;

Go ahead and give it a try.

by Phil for Humanity
on 20100422

Related Articles
 » How to Fork a Child Process and Wait in Perl
 » UNIX Support: Recursively GREP
 » Command Loop