Say what you have
$ cat file.txt This line has $variable DO NOT PRINT ME! $variableNope $variable also
Then with the next program
#! /usr/bin/perl -l use warnings; use strict; system("grep", "-P", '\$variable\b', "file.txt") == 0 or warn "$0: grep exited " . ($? >> 8);
you will get a conclusion
This line has $ variable
$ variable also
It uses the -P switch in GNU grep , which matches Perl regular expressions. The function is still experimental, so proceed with caution.
Also note the use of system LIST , which bypasses shell quotation marks, allowing the program to specify arguments with citation rules in Perl rather than with the shell.
You can use the -w (or --word-regexp ), as in
system("grep", "-w", '\$variable', "file.txt") == 0 or warn "$0: grep exited " . ($? >> 8);
to get the same result.
source share