exact match in Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting exact match in Perl
# 1  
Old 05-12-2010
Power exact match in Perl

Hi

By using select clause I'm trying to pull out the rows to a variable.
If the variable has 0 row(s) selected then i'm printing some text message
else printing some other text message

Code:
if($xyz =~ m/0 row/)
{ 
  print "0 rows ";
}
else
{ 
  print " There are rows";
}

By my problem is if the variable has 20 row(s) in it ..it is printing as 0 rows as it's matching 0 in it...

I need to match the exact word '0 row'

Can anyone help me please?

Last edited by Scott; 05-12-2010 at 11:42 PM.. Reason: Code tags, please...
# 2  
Old 05-12-2010
What is the value of $xyz ?

If it's simply "20 row(s)" or "0 row(s)", you could use:
Code:
/^0 row/

If it's something like "Found 20 row(s)" or "Found 0 row(s)" following should work:
Code:
/ 0 row/

# 3  
Old 05-12-2010
Thanks for the reply..

The value of the variable keeps changing say 0 row(s) selected,21 row(s) selected,10 row(s) selected,,20 row(s) selected etc...
I want to print "0 rows" when there are 0 rows in the variable
other wise the no of rows in the variable.
# 4  
Old 05-13-2010
You're welcome.
In that case you can safely use m/^0 row/, it matches if the variable starts with 0 row.
This User Gave Thanks to pseudocoder For This Post:
# 5  
Old 05-13-2010
Perl Exact Match

The Best way in Perl to get an exactly exact match is to use the eq operator ( for strings ) or the == operator ( for numbers ), not a regex.

Code:
if  ( $xyz eq '0 rows' ) { .... }

If you need the flexibility the regex, this idiom is more precise:

Code:
if ( $xyz =~ /^\b0 rows\b/ ) { ... }  else { .... }

the "\b" 's ( zero-width boundary match ) which surround the string make sure that it will not match overlapping or embedded things.

Consider this:

Code:
use strict;
use warnings;

$_ = 'contentious';
print "HIT, NO BOUNDARY\n" if /tent/;
print "HIT, WITH BOUNDARY\n" if /\btent\b/;

For me this outputs:

Code:
HIT, NO BOUNDARY

Hope that Helps.

Last edited by deindorfer; 05-13-2010 at 01:10 AM.. Reason: Misspelling
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Help match the exact string

I just want to match "binutils1_test" only, and print the match line only lyang001@lyang001-OptiPlex-9010:/tmp$ cat file zbinutils1_test bbinutils1_test binutils1_test w-binutils1_test lyang001@lyang001-OptiPlex-9010:/tmp$ cat file |grep -w 'binutils1_test' ... (7 Replies)
Discussion started by: yanglei_fage
7 Replies

3. Shell Programming and Scripting

Grep exact match

Hello! I have 2 files named tacs.tmp and tacDB.txt tacs.tmp looks like this 0 10235647 102700 106800 107200 1105700 tacDB.txt looks like this 100100,Mitsubishi,G410,Handheld,,0,0,0 100200,Siemens,A53,Handheld,,0,0,0 100300,Sony Ericsson,TBD (AAB-1880030-BV),Handheld,,0,0,0... (2 Replies)
Discussion started by: Cludgie
2 Replies

4. Shell Programming and Scripting

Match exact and append zero

file 11 2 12 6 13 7 114 6 011 7 if I'm searching for 11, output needed is output: 11 2 011 7 Code: awk '$1 ~ /^11$/' file I used the above to match exact, but it avoiding "011 7" line too, how to resolve this? (6 Replies)
Discussion started by: Roozo
6 Replies

5. Shell Programming and Scripting

Exact match and #

Hi friends, i am using the following grep command for exact word match: >echo "sachin#tendulkar" | grep -iw "sachin" output: sachin#tendulkar as we can see in the above example that its throwinng the exact match(which is not the case as the keyword is sachin and string is... (6 Replies)
Discussion started by: neelmani
6 Replies

6. Shell Programming and Scripting

PERL: Help required exact match

Hello, My requirement is to iterate over all the lines of a file and compare them with a word and perform some operations if exact match is found. For the snippet below, it works even if contents of line include "diff" and "diff:". I want it to work only if it is exactly "diff" and is not... (2 Replies)
Discussion started by: sarbjit
2 Replies

7. Shell Programming and Scripting

Issues with an exact match using regex in perl!

Hello Guys, I am trying to make an exact match for an email address entered as an argument, using perl, however, it's not working if I put a "$" in the email address. See the below outputs, Correct Match : bash-2.03$ echo sandy@test.com | perl -wln -e 'print if /(^*\@test.com$)/i'... (6 Replies)
Discussion started by: suffisandy
6 Replies

8. Shell Programming and Scripting

exact string match ; search and print match

I am trying to match a pattern exactly in a shell script. I have tried two methods awk '/\<mpath${CURR_MP}\>/{print $1 $2}' multipath perl -ne '/\bmpath${CURR_MP}\b/ and print' /var/tmp/multipath Both these methods require that I use the escape character. I am guessing that is why... (8 Replies)
Discussion started by: bash_in_my_head
8 Replies

9. Shell Programming and Scripting

Exact Word Match

I'm trying to find a exact word match but couldn't do it. ABC ABC_NE Searching for ABC_NE tried grep -w </ABC_NE/> grep "^ABC_NE$" but didn't worked , any awk variants would also help. ---------- Post updated at 08:40 AM ---------- Previous update was at 06:48 AM ---------- I... (2 Replies)
Discussion started by: dinjo_jo
2 Replies

10. Shell Programming and Scripting

perl exact match

How to emulate grep -o option in perl. I mean to print not all line, only the exact match. echo "2A2 BB" | perl -ne 'print if /2A2/' 2A2 BB I want to print only 2A2. (2 Replies)
Discussion started by: mirusnet
2 Replies
Login or Register to Ask a Question