tags:

views:

823

answers:

7

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.

A: 

Using single quotes works for me in tcsh:

grep '<$variable>' file.txt

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

toolic
The `\<` and `\>` are regex specifiers for word boundaries.
Dennis Williamson
That doesn't expand '$variable' which the user requested. It would also require the exact string, with '<' before and '>' after the text.
Jonathan Leffler
I think Guaurav needs to show a small sample of his input file, his desired output, and confirm that `$variable` should be expanded by the shell. When more details become available, I'll update my answer.
toolic
I've updated the question, have a look.
Gaurav
A: 

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.

Dennis Williamson
Trying to grep for 'foo', but not showing 'food'.
Jonathan Leffler
@Jonathan: I know. And it works for me using GNU grep 2.5.4
Dennis Williamson
+1  A: 

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?

Mala
I've updated the question, have a look.
Gaurav
ah, my apologies
Mala
A: 

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

stacker
+1  A: 

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

I think I would just do:

grep -w "$variable" ...
DigitalRoss
I think you probably meant to use double quotes there.
Mike Nelson
A: 

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.

Greg Bacon
+4  A: 

@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";
    }
 }
}
ghostdog74
Quite. In particular, with system commands, you have to worry about which version of the command is installed. Perl is there to make these worries go away.
Charles Stewart