Operating System - HP-UX
1831167 Members
2846 Online
110021 Solutions
New Discussion

Perl pattern searching - help needed

 
SOLVED
Go to solution
Anand_30
Regular Advisor

Perl pattern searching - help needed

Hi,

I have a perl statement in a UNIX Shell script. I am using the perl statement for pattern maching.I have got the pattern in an UNIX variable 'x'. I want to use this variable for the pattern searching in the perl statement which is like:

perl -ne 'if (/pattern/) {.....}

I want the pattern to have the value of 'x'. Is it possible.

Thanks,
Andy
7 REPLIES 7
Karthik S S
Honored Contributor

Re: Perl pattern searching - help needed

You can use $x,

i.e.

perl -ne 'if (/$x/) {.....}

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Anand_30
Regular Advisor

Re: Perl pattern searching - help needed

Thanks for the reply Karthik,

I have tried using $x but it doesn't work.

-Anand
Karthik S S
Honored Contributor

Re: Perl pattern searching - help needed

Anand,

I think the following link might help you,

http://zamov.online.fr/EXHTML/Perl/Perl_27283.html

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Karthik S S
Honored Contributor

Re: Perl pattern searching - help needed

Anand,

I tried the following and it works,


---------

#!/usr/bin/perl

if ( $#ARGV != 1 )
{
print "Usage: test.pl string_to_searched string\n";
}
else
{
$x = $ARGV[0];
$test = $ARGV[1];

if ( $test =~ /$x/ )
{
print "String contains $x in it! \n";
}
else
{ print "I am Sorry!! $x is not found in string \n";
}
}

--------------

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
John Wright_1
Advisor
Solution

Re: Perl pattern searching - help needed

Hi,

$ myvar="Hello World!"
$ export myvar
$ perl -e 'print $ENV{myvar} . "\n";'
Hello World!
$

Cheers,
JW.

John Wright_1
Advisor

Re: Perl pattern searching - help needed

Hi again,

Here is an example with pattern matching.

[dev] # x=xyz
[dev] # export x
[dev] # perl -e 'if ("uvwxyz" =~ /$ENV{var}/){print "match\n";}else{print "no match\n";}'
match
[dev] # perl -e 'if ("uvwyz" =~ /$ENV{var}/){print "match\n";}else{print "no match\n";}'
no match
[dev] #

Cheers,
JW.
John Wright_1
Advisor

Re: Perl pattern searching - help needed

I don't believe I did that. No points.

[dev] # x=xyz
[dev] # export x
[dev] # echo $x
xyz
[dev] # perl -e 'print "uvwxyz" =~ /$ENV{x}/ ? "Yes\n" : "No\n";'
Yes
[dev] # perl -e 'print "uvwyz" =~ /$ENV{x}/ ? "Yes\n" : "No\n";'
No
[dev] #


JW.