find matching pattern in perl using command line and output matching itself (if found).

How to find match in perl using command line?
Okay, I can even substitute using

perl -i.bak -pe 's/pattern1/pattern2/g' inputFile

How to just find match and output catches?
perl -pe 'if ($_ =~ m/$.+?src(.+?)^/) {print "$1\n"}' inputFile
Nusrat NuriyevAsked:
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:
perl -pe 'print if /pattern1/' inputFile
perl -lpe 'print $1 if /src(.+?)/' inputFile
0
Nusrat NuriyevAuthor Commented:
perl -pe 'print if /pattern1/' inputFile

It outputs all lines no matter what pattern is given.
0
Nusrat NuriyevAuthor Commented:
perl -lpe 'print $1 if /src(.+?)/' inputFile


also outputs all and  whole lines , no matter of pattern

Generally
perl -pe '' inputFile
outputs all content of file is
Is that expected behavior?
As I understand if no code, then it just iterates but should not output.

the behaviour of -p:

while (<>) {
   ... # your script
} continue {
   print or die "-p destination: $!\n";
}
0
ozoCommented:
perl -ne 'print if /pattern1/' inputFile
perl -lne 'print $1 if /src(.+?)/' inputFile
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
Nusrat NuriyevAuthor Commented:
Notice the use of the empty file input operator, which will read all of the files given on the command line a line at a time. Each line of the input files will be put, in turn, into $_ so that you can process it. As a example, try:

  $ perl -n -e 'print "$. - $_"' file

This gets converted to:

  LINE:
    while (<>) {
      print "$. - $_"
    }

This code prints each line of the file together with the current line number.

The -p option makes that even easier. This option always prints the contents of $_ each time around the loop. It creates code like this:

  LINE:
    while (<>) {
      # your code goes here
    } continue {
      print or die "-p destination: $!\n";
    }
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.