Gaurav
Gaurav

Reputation: 157

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.

Upvotes: 4

Views: 19295

Answers (8)

Mariappan Subramanian
Mariappan Subramanian

Reputation: 10063

Using single quote it wont work. You should go for double quote

For example:

this wont work
--------------
for i in 1
do
grep '$i' file
done

this will work
    --------------
    for i in 1
    do
    grep "$i" file
    done

Upvotes: 0

DigitalRoss
DigitalRoss

Reputation: 146053

Not every grep supports the ex(1) / vi(1) word boundary syntax.

I think I would just do:

grep -w "$variable" ...

Upvotes: 1

ghostdog74
ghostdog74

Reputation: 342323

@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";
    }
 }
}

Upvotes: 4

Greg Bacon
Greg Bacon

Reputation: 139441

Say you have

$ cat file.txt
This line has $variable
DO NOT PRINT ME! $variableNope
$variable also

Then with the following program

#! /usr/bin/perl -l

use warnings;
use strict;

system("grep", "-P", '\$variable\b', "file.txt") == 0
  or warn "$0: grep exited " . ($? >> 8);

you'd get output of

This line has $variable
$variable also

It uses the -P switch to GNU grep that matches Perl regular expressions. The feature is still experimental, so proceed with care.

Also note the use of system LIST that bypasses shell quoting, allowing the program to specify arguments with Perl's quoting rules rather than the shell's.

You could use the -w (or --word-regexp) switch, as in

system("grep", "-w", '\$variable', "file.txt") == 0
  or warn "$0: grep exited " . ($? >> 8);

to get the same result.

Upvotes: 0

stacker
stacker

Reputation: 68942

On a recent linux it works as expected. Do could try egrep instead

Upvotes: 0

Mala
Mala

Reputation: 14803

I'm not seeing the problem here:

file.txt:

hello
hi
anotherline

Now,

mala@human ~ $ export GREPVAR="hi"
mala@human ~ $ echo $GREPVAR
hi
mala@human ~ $ grep "\<$GREPVAR\>" file.txt 
hi

What exactly isn't working for you?

Upvotes: 1

Dennis Williamson
Dennis Williamson

Reputation: 359955

If variable=foo are you trying to grep for "foo"? If so, it works for me. If you're trying to grep for the variable named "$variable", then change the quotes to single quotes.

Upvotes: 0

toolic
toolic

Reputation: 62037

Using single quotes works for me in tcsh:

grep '<$variable>' file.txt

I am assuming your input file contains the literal string: <$variable>

Upvotes: 0

Related Questions