Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

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
0
Nusrat Nuriyev
Asked:
Nusrat Nuriyev
  • 3
  • 2
1 Solution
 
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
 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now