-3

Possible Duplicate:
perl + one line smart perl command, in place grep to match unusual characters + full match

Ai all

I need to match exactly the PARAMETER string with the following perl line:

 cat file  | perl -nle 'print if /\Q$ENV{PARAMETER}/'

As the following examples, showing down

I try to match: node_name

but I get all node_name combinations names from the file,

The same about 1.1.1. and host_1.A etc…

how to match exactly the following PARAMETERS ? from the file , what need to change in my perl syntax in order to give the right match?

 more file
 param1=uplicateParam node_name
 param2=a anode_name 
 param3=bnode_name 
 param4node_name 
 param5=1.node_name 
 param6=11.11.11.11  
 param7=1.1.1.11
 param8=[1234]
 param9=* * * [@]
 param10=11.1.1.11
 param11=host_1.A
 param12=old.host_1.A


 example1

 PARAMETER=node_name
 export  PARAMETER
 cat file  | perl -nle 'print if /\Q$ENV{PARAMETER}/'

 DuplicateParam node_name
 a anode_name 
 bnode_name 
 node_name 
 1.node_name 


 Example2


 PARAMETER=1.1.1.1
 export  PARAMETER
 cat file  | perl -nle 'print if /\Q$ENV{PARAMETER}/'

 param7=1.1.1.11
 param10=11.1.1.11

 example3

 PARAMETER=host_1.A
 cat file  | perl -nle 'print if /\Q$ENV{PARAMETER}/'
 export  PARAMETER

 host_1.A
 old.host_1.A
Community
  • 1
  • 1
jon
  • 903
  • 5
  • 14
  • 21
  • not duplicate because this question is about perl match , second I dont get answer , third I must give more examples – jon Nov 18 '10 at 06:27

3 Answers3

0

How can This line:

param2=a anode_name

being grepped for node_name yield a anode_name

while this line:

param7=1.1.1.11

being grepped for 1.1.1.1 yield param7=1.1.1.11

It seems to me that either the first should yield param2=a anode_name or the second should yield 1.1.1.11

tster
  • 17,883
  • 5
  • 53
  • 72
  • @rster - what need to change in my perl syntax in order to give the right match? – jon Nov 18 '10 at 06:30
  • @jon, I know this isn't the answer, because you are asking for contradictory output. I personally think you should do `print $1 if (/^param\d+=(.*($ENV{PARAMETER}).*)/` – tster Nov 18 '10 at 06:34
0

You should try to put word boundaries (\b) bto match exactly:

cat file  | perl -nle 'print if /\b\Q$ENV{PARAMETER}\b/'
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
  • 1
    Careful: boundaries specified with `\b` often fail to do [what most people think they do](http://stackoverflow.com/questions/4213800/is-there-something-like-a-counter-variable-in-regular-expression-replace/4214173#4214173). – tchrist Nov 18 '10 at 13:31
0
perl -nlE 'say if /\Aparam\d+=\Q$ENV{PARAMETER}\E\s*\z/' file
sid_com
  • 24,137
  • 26
  • 96
  • 187