Computing.Net > Forums > Unix > Unix equivalent of Like operator

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Unix equivalent of Like operator

Reply to Message Icon

Name: anukta_c
Date: July 24, 2003 at 08:36:23 Pacific
OS: HP-UX
CPU/Ram: N/A
Comment:

Hi,
This is probably a very simple question.
I cannot remember what is the shell equivalent for the Like operator.

I want to construct a syntax like this:

var=er_non_pstn_output.csv
if $var like '*non_pstn*'
do something
else
do something
fi

Which is to say if the variable var contains the pattern "non_pstn" somewhere, do something.

I know the solution is very easy, but simply cannot remember how this is done.
Can anyone help?
-Anukta



Sponsored Link
Ads by Google

Response Number 1
Name: James Boothe
Date: July 24, 2003 at 11:44:01 Pacific
Reply:

Colon of expr command is the like operator.  If pattern is not found, it returns zero (and false status), otherwise it returns length of the matching portion and true status. The pattern has an automatic beginning-of-line anchor, and you can optionally put an end-of-line anchor.

expr "$var" : ".*non_pstn"
11

if expr "$var" : ".*non_pstn" 2> /dev/null ; then
   echo 'Pattern found'
else
   echo 'Pattern NOT found'
fi


0

Response Number 2
Name: anukta_c
Date: July 25, 2003 at 03:00:47 Pacific
Reply:

Thanks James, that worked.

I did notice that it works even without a wildcard character.

if expr "$var":"non_pstn" 2> /dev/null
then
echo PATTERN FOUND
else
echo NOT FOUND
fi

works as well.
-Anukta


0

Response Number 3
Name: anukta_c
Date: July 25, 2003 at 03:39:48 Pacific
Reply:

Looks like a little problem here:
For the value

var=er_non_pstn_output.csv
expr "$line":".*non_pstn"

pattern matches very well.Which is fine.
But when

var=er_pstn_output.csv

I don't want the pattern match to return true, but it does.
How can I get around this problem?
-Anukta



0

Response Number 4
Name: WilliamRobertson
Date: July 25, 2003 at 06:18:31 Pacific
Reply:

In ksh there's no "like", you just use "=" with pattern matching:

[[ ${var} = *non_pstn* ]] && print Yep


0

Response Number 5
Name: anukta_c
Date: July 25, 2003 at 06:53:56 Pacific
Reply:


William,
That seems to work, Thanks a lot.

Can you also tell me when do you use a single '[' bracket for comparison and when do you use a double '[['?

-Anukta


0

Related Posts

See More



Response Number 6
Name: WilliamRobertson
Date: July 25, 2003 at 07:48:11 Pacific
Reply:

[ ] is bourne shell, which ksh supports for backward compatibility. [[ ]] is ksh syntax (also bash). I always use [[ ]].


0

Response Number 7
Name: James Boothe
Date: July 25, 2003 at 08:23:51 Pacific
Reply:

anukta,
Regarding your little problem, I see two problems. First, you are setting a value for $var but then testing $line. And secondly, that colon needs to be space delimited.

In the first example below, expr simply sees a unary string expression and displays the string as is (the shell strips the quotes before expr gets it). The second example finds no match because there is an implied beginning-of-line anchor:

expr abcdefgh:"def"
abcdefgh:def

expr abcdefgh : "def"
0

expr abcdefgh : ".*def"
6


0

Response Number 8
Name: anukta_c
Date: July 25, 2003 at 09:10:52 Pacific
Reply:


That clears it up. Thanks

sorry about mixing up the $var and $line, I meant to write $var of course. Copy-pasted that line from the original code.

Thanks to both of you for helping me out.
-Anukta


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Unix equivalent of Like operator

Equivalent of continue in If www.computing.net/answers/unix/equivalent-of-continue-in-if/6833.html

Where do I find a good UNIX version... www.computing.net/answers/unix/where-do-i-find-a-good-unix-version/2689.html

Disaster Recovery of Unix www.computing.net/answers/unix/disaster-recovery-of-unix/954.html