Perl Regular expression

I have a perl script that I want to  take 2 options just like Unix grep

-v negates the match; print only lines that do not match the regex
-i makes it case sensitive; match regardless of case

Either or both options can be present or not, in any order, but if present, must occur before the rest of the arguments.
For example: .
/reg_grep2.pl -v perl ./*.pl
or ./reg_grep2.pl -i -v Test ./*.test.

I have tried the attached script, however it is not working properly.

Can anyone give me the correct script or modify existing one to make it work.


reg-grep2
clock_crazyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
What does -i patteren -v patteren mean?
0
clock_crazyAuthor Commented:
I have provided examples in commented part ( IN script )

#Example: ./reg_grep2.pl -v perl ./*.pl
 #Example: ./reg_grep2.pl -i -v Test ./*.test.

-v pattern : negates the match; print only lines that do not match the pattern ( e.g perl or any word )
-i : should not have any pattern; makes it case sensitive; match regardless of case

( Just like Unix grep command with grep -v and grep -i )
0
ozoCommented:
Assuming that you have only one pattern and that the -i and -v take no arguments
so that reg_grep.pl -i patteren -v patteren filename would mean that -v patteren filename are all considered to be file names
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;
use vars qw( $opt_v $opt_i );

#my %opts;

if( !getopts('vi') )
{
  die "Usage: reg_grep.pl [-iv] patteren filename\n";
  #Example: ./reg_grep2.pl -v perl ./*.pl
  #Example: ./reg_grep2.pl -i -v Test ./*.test.
}

my $regex = shift;
($opt_i&&='(?i)')||='';
while( <> ){
    print if /$opt_i$regex/ ^ !!$opt_v;
}

Open in new window

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

clock_crazyAuthor Commented:
would you explain the code to me;

my $regex = shift;                                 # Take Argument from command prompt
($opt_i&&='(?i)')||='';                            #  ??  ( assigning default value of ' ' to $opt_i  && ??????????
while( <> ){                                             #  read default $_
    print if /$opt_i$regex/ ^ !!$opt_v;          # ????????????????????
}


Also, what was wrong about my script ?
0
clock_crazyAuthor Commented:
Can you give me a script that anyone can understand ?
0
ozoCommented:
($opt_i&&='(?i)')||=''; is equivalent to $opt_i = $opt_i?'(?i)':'';
!!$opt_v is like ($opt_v||'') in that it does not throw a Use of uninitialized value warning when $opt_v is undefined
what was wrong about my script
When $opt_v is set, it ignores $opt_i, and when neither is set, it acts as if  $opt_v was set
It also gives a misleading Usage: message
0
clock_crazyAuthor Commented:
I am going to accept your solution and take a bow !!!!

I have last question: can you please explain $opt_i = $opt_i?'(?i)':'' , is this combination of ternary operator and positive look ahead expression  and how come caret ( ^ ) symbol before !!$opt_v ?
0
ozoCommented:
$opt_i = $opt_i?'(?i)':''
means
if( $opt_i ){
   $opt_i = '(?i)';
}else{
    $opt_i = '';
}
when used in /$opt_i$regex/, (?i) means do case-insensitive pattern matching
see `perldoc perlre`

^ is the Exclusive Or operator
see `perldoc perlop`
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
clock_crazyAuthor Commented:
Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.