Search code examples
regexshellperlgrep

How can I grep for a value from a shell variable?


I've been trying to grep an exact shell 'variable' using word boundaries,

grep "\<$variable\>" file.txt

but haven't managed to; I've tried everything else but haven't succeeded.

Actually I'm invoking grep from a Perl script:

$attrval=`/usr/bin/grep "\<$_[0]\>" $upgradetmpdir/fullConfiguration.txt`

$_[0] and $upgradetmpdir/fullConfiguration.txt contains some matching "text".

But $attrval is empty after the operation.


Solution

  • @OP, you should do that 'grepping' in Perl. don't call system commands unnecessarily unless there is no choice.

    $mysearch="pattern";
    while (<>){
     chomp;
     @s = split /\s+/;
     foreach my $line (@s){
        if ($line eq $mysearch){
          print "found: $line\n";
        }
     }
    }