LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-03-2014, 01:00 AM   #1
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Rep: Reputation: Disabled
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);
 
Old 12-03-2014, 02:55 AM   #2
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu
Posts: 1,532

Rep: Reputation: 132Reputation: 132
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.
 
Old 12-03-2014, 05:10 AM   #3
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled
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.
 
Old 12-03-2014, 05:15 AM   #4
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu
Posts: 1,532

Rep: Reputation: 132Reputation: 132
perldoc perlfunc

says that it does not have such options
 
Old 12-03-2014, 10:01 AM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,599

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
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.

Last edited by jpollard; 12-03-2014 at 10:03 AM.
 
Old 12-04-2014, 04:01 AM   #6
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled
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.
 
Old 12-04-2014, 04:09 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
Quote:
Originally Posted by jags1984 View Post
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)
 
Old 12-04-2014, 04:59 AM   #8
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled
I am looking for solution in this way.... and not the line matching way.
 
Old 12-04-2014, 05:52 AM   #9
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,599

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Well, you won't find it.
 
Old 12-05-2014, 06:33 AM   #10
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled
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 !
 
Old 12-05-2014, 06:45 AM   #11
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu
Posts: 1,532

Rep: Reputation: 132Reputation: 132
Quote:
Originally Posted by jags1984 View Post
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.

Last edited by j-ray; 12-05-2014 at 06:46 AM.
 
Old 12-06-2014, 10:53 AM   #12
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
Quote:
Originally Posted by j-ray View Post
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.
 
Old 12-08-2014, 06:16 AM   #13
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled
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.

Last edited by jags1984; 12-08-2014 at 07:42 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] grep with pattern and line number mohitvad Linux - Newbie 1 09-14-2012 05:00 PM
Perl One Liner : Capturing more than one matched regex per line and printing uncle-c Programming 6 01-22-2012 05:16 AM
[SOLVED] Sed - print line number along with the line ? ntpntp Linux - Newbie 6 01-30-2011 06:58 AM
Want to print fouth line after resulted line using grep with tail command saurabhmehan Linux - Newbie 10 08-05-2010 12:38 AM
start grep at line number secretlydead Linux - General 2 09-20-2007 07:03 AM


All times are GMT -5. The time now is 05:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration