LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Searching line by line in perl from root. (https://www.linuxquestions.org/questions/linux-newbie-8/searching-line-by-line-in-perl-from-root-4175527753/)

jags1984 12-08-2014 11:01 PM

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]+)\)';

jpollard 12-09-2014 07:16 AM

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?

jags1984 12-10-2014 12:49 AM

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);
}

jpollard 12-10-2014 04:22 PM

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.


All times are GMT -5. The time now is 12:38 AM.