LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to print the matched line number using perl grep (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-print-the-matched-line-number-using-perl-grep-4175527156/)

jags1984 12-03-2014 12:00 AM

How to print the matched line number using perl grep
 
How to print a line number of the matched pattern using perl grep.


I am doing the following , I can print the matched line but not the line number.

Code:

open $INPUT, '<', $filename
my @found=grep ( /$search_pattern/, <$INPUT>) ;
foreach my $line (@found) {
              print $filename, ":", $line;
      }
      close($INPUT);


j-ray 12-03-2014 01:55 AM

I guess you have to loop through all lines with a counter, do a grep on each line and if it matches you have the line number and the matching line.

jags1984 12-03-2014 04:10 AM

Yes setting a line counter and searching line by line is a quick solution.

I wanted to check whether perl grep has any option for line numbers as Unix grep -n.

j-ray 12-03-2014 04:15 AM

perldoc perlfunc

says that it does not have such options

jpollard 12-03-2014 09:01 AM

Not this way.

The problem is that <$INPUT> is treated as an array construction and will read the entire file into a buffer, then the grep will search that buffer - thus no line numbers are available.

You can get what you want - but using:
Code:

open $INPUT, '<', $filename;
while (my $line = <$INPUT>) {
        $line =~ /$search_pattern/ && print $filename, ":", $.,":",$line;
      }
      close($INPUT);

The "$." variable is the line number from the current input - in this case, from the <$INPUT>. Here, the <$INPUT> is being used in a scalar context (the "my $line =") so it is not treated as an array and reading the entire file at once.

jags1984 12-04-2014 03:01 AM

I dont want to do line by line comparison, thats why I moved to perl grep .... I am looking for a workaround if no option is provided by perl grep.

pan64 12-04-2014 03:09 AM

Quote:

Originally Posted by jags1984 (Post 5279245)
I dont want to do line by line comparison

What do you mean by that?
If you want a matched pattern you must do line by line comparison. How else can it be done? (The only difference is how do you organize the loop)

jags1984 12-04-2014 03:59 AM

I am looking for solution in this way.... and not the line matching way.

jpollard 12-04-2014 04:52 AM

Well, you won't find it.

jags1984 12-05-2014 05:33 AM

Well, Thanks for your valuable time.

I will go with line by line option.

This can be one of the shortcomings of PERL compare to shell script.



Have a good day !

j-ray 12-05-2014 05:45 AM

Quote:

Originally Posted by jags1984 (Post 5279917)
This can be one of the shortcomings of PERL compare to shell script.

You can use grep within perl as well as in a shell script

my @oh = `grep -n PATTERN FILENAME`;
print @oh;


So I guess there is no real shortcoming.

pan64 12-06-2014 09:53 AM

Quote:

Originally Posted by j-ray (Post 5279925)
You can use grep within perl as well as in a shell script

my @oh = `grep -n PATTERN FILENAME`;
print @oh;


So I guess there is no real shortcoming.

that will fork two additional processes without giving any [additional] advantage (that grep can be easily implemented in perl). Not to speak about the fact: every solution will use line by line comparison, the only difference is that you will implement it or you will use an already implemented one.

jags1984 12-08-2014 05:16 AM

I am using line by line option now and tried searching pattern from root (/)

I am getting the following error

Quote:

Use of uninitialized value $Name in substr at /usr/share/perl5/File/Find.pm
line 386 (#1)
(W uninitialized) An undefined value was used as if it were already
defined. It was interpreted as a "" or a 0, but maybe it was a mistake.
To suppress this warning assign a defined value to your variables.

To help you figure out what was undefined, perl will try to tell you the
name of the variable (if any) that was undefined. In some cases it cannot
do this, so it also tells you what operation you used the undefined value
in. Note, however, that perl optimizes your program and the operation
displayed in the warning may not necessarily appear literally in your
program. For example, "that $foo" is usually optimized into "that "
. $foo, and the warning will refer to the concatenation (.) operator,
even though there is no . in your program.

Use of uninitialized value $fn in string eq at /usr/share/perl5/File/Find.pm
line 367 (#1)
Use of uninitialized value $fn in substitution (s///) at
/usr/share/perl5/File/Find.pm line 371 (#1)
Use of uninitialized value $fn in concatenation (.) or string at
/usr/share/perl5/File/Find.pm line 373 (#1)
Use of uninitialized value $fn in substr at /usr/share/perl5/File/Find.pm line
375 (#1)
My search pattern is $search_pattern = 'fnOpen\(([\w&\*\s]+)\,([\w&\*\s]+)\)';




PS: Yes, grep with backticks will create additional process.


All times are GMT -5. The time now is 12:51 PM.