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-08-2014, 11:01 PM   #1
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Rep: Reputation: Disabled
Searching line by line in perl from root.


Hi,

I am searching the pattern and printing the filename and linenumber using the below code.

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

It works fine, but when i tried searching from root (/) it is giving below 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)

The search pattern I am giving is
Code:
$search_pattern = 'fnOpen\(([\w&\*\s]+)\,([\w&\*\s]+)\)';
 
Old 12-09-2014, 07:16 AM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,688

Rep: Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259
I'm not certain...

But it is possible that due to the reinterpretation of the pattern that the & character in the pattern is causing a function call. You might try using the pattern:

'fnOpen\(([\w\&\*\s]+)\,([\w\&\*\s]+)\)'

The & is generally used to cause the perl interpreter to treat the following token to be a function.

This doesn't quite feel right to me, but then I've not had to use the & character within a character class before.

One other question, why are you trying to set $1 and $2 (the unescaped "()") to the parameter names of the function being searched for?
 
Old 12-10-2014, 12:49 AM   #3
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled
Quote:
One other question, why are you trying to set $1 and $2 (the unescaped "()") to the parameter names of the function being searched for?

So that I could replace the parameters, this is one of the requirement.


The problem persists even if i use a simple search pattern.

Like if I use $search_pattern= "Install" and do a find from a root.

I found the problem is because of the follow (use to resolve symbolic links) used in find command.

It works fine now without follow but the find doesnt resolve the symbolic links .

Sorry I forgot to paste the whole code.
Quote:
find( { wanted => \&Action,
#follow => 1,
follow_skip => 2, # to ignore any duplicate files and directories but to proceed normally otherwise
no_chdir => 1, # Does not chdir() to each directory as it recurses
}, $out_dir);
sub Action{
my $INPUT;
my $filename;
my $filename= $File::Find::name;

return unless -f

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

Last edited by jags1984; 12-10-2014 at 12:55 AM.
 
Old 12-10-2014, 04:22 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,688

Rep: Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259
To me, that would sound like a bug in find.

I scanned it and didn't see anything that would change the global pattern match operators - but I could have missed it.
 
  


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
Perl script to replace a line after finding a previous line Mark1986 Programming 1 02-28-2011 05:09 PM
Perl: Match part of a line and replace with another line from the same file briana.paige Linux - Newbie 8 06-27-2009 06:35 AM
Perl question: delete line from text file with duplicate match at beginning of line mrealty Programming 7 04-01-2009 06:46 PM
grab the line below a blank line and the line above the next blank line awk or perl? Pantomime Linux - General 7 06-26-2008 08:13 AM


All times are GMT -5. The time now is 02:23 PM.

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