It is currently Tue, 17 May 2022 18:39:58 GMT



 
Author Message
 string matching in ksh
I'm trying to understand string matching from the ksh man page.
In ksh, why does the following happen?

# [ "abc"="^b.*" ] && echo yes
yes
#
# [ "abc" = "^b.*" ] && echo yes
#
# [ "abc" = "^a.*" ] && echo yes
#



 Mon, 13 Oct 2003 10:19:54 GMT   
 string matching in ksh
In article <3AE785CA.B9FDF...@to.gd-es.com>,
Allen M. Cohen <a...@to.gd-es.com> wrote:

This happens for the same reason that

$ [[ no ]] && print yes
yes

happens.

ksh patterns and regular expressions are different things.
However I do not know why they should be different. After
writing so many ksh patterns, I can not write regular
expressions any more.



 Mon, 13 Oct 2003 11:40:11 GMT   
 string matching in ksh
Your first line is understood as one string: 'abc=^b.*' which is true.
When you put a space around the '=' it does an evaluation and in shell
'abc' does not equal '^b.*' which is a regular expression and not
expanded by the shell.  The equivalent of .* in shell is plain * and
there is no equivalent to ^ for indicating the start of a line.


 Mon, 13 Oct 2003 21:50:37 GMT   
 string matching in ksh
In ksh,  you can do complicated pattern matching using
conditional expressions,  which look a little like test
statements but are vastly superior.

In a test statement,  the "[" acts as a synonym for test with
one change,  it requires a closing "]".  It is treated as a
command,  just as if you had written "test" or "/bin/test".  
Command line expansion occurs with all attendant perils.

In a conditional expression,  consisting of a statement bracketted
by "[[" and "]]",  the "[[" and "]]" elements are keywords,  just like
"case" and "esac".  And,  just as you never need to quote the tested
variable in a case statement you never need to quote the left hand
side of a conditional expression:

   case $var in pattern) ;; pattern) ;; esac

works for all values of $var.

   case "$var" in pattern) ;; pattern) ;; esac

is never necessary.  Similarly:

   [[ $var = whatever ]]
and
   [[ -f $var ]]

work for all values of $var.

In addition,  filename substitution is no performed in a
conditional expression (well,  except for one release of ksh
put out by HP-UX (;-)).  The globbing characters are used to match
patterns:

   abc=abc
   if [[ $abc = b* ]];then ...

or

   if [[ $abc = ?b* ]];then ...

In addition,  there is an extended pattern notation.  For instance:

   if [[ $abc = !(def|ghi) ]]

will be true for all values of $abc that do not match "def" or
"ghi". You can easily construct very complicated match
expressions and do things that are impossible for regular
expressions.  Ksh93 provides an additional mechanism using the
builtin "printf" command to generate extended pattern notation from
regular expressions:

      $ re=$(printf "%P\n" "(abc|def)")
      $ echo $re
      *@(abc|def)*
      $ for i in abc def ghi 1abc2 2def cabc
      > do
      > print -n "$i: "
      > if [[ $i == $re ]];then print yes;else print no;fi
      > done
      abc: yes
      def: yes
      ghi: no
      1abc2: yes
      2def: yes
      cabc: yes
      $ re=$(printf "%P\n" "^(abc|def)")
      $ echo $re
      $ for i in abc def ghi 1abc2 2def cabc
      > do
      > print -n "$i: "
      > if [[ $i == $re ]];then print yes;else print no;fi
      > done
      @(abc|def)*
      abc: yes
      def: yes
      ghi: no
      1abc2: no
      2def: no
      cabc: no

(ksh93 supports "==" for testing equality).

--
Dan Mercer
damer...@mmm.com

In article <3AE827AD.2AD64...@noaa.gov>,

Opinions expressed herein are my own and may not represent those of my employer.



 Tue, 14 Oct 2003 00:12:45 GMT   
 string matching in ksh
Thanks for the replies...

I guess the ksh man page in **Solaris 8** is incorrect!  It says:
    string = pattern
        True, if string matches pattern
    string1=string2         (NO SPACE AROUND EQUALS!!!)
        True if the strings string1 and string2 are identical
Maybe they meant "==" ???

I now understand that "pattern" means a very simplied, file type, regex
as used in case statements, etc.

ONE OTHER QUESTION:  It seems that ksh supports the "test for sym link"
operator "-h" (which is undocumented) for compatiblity with test/sh;
moreover that "-h" and "-L" are the same operators.  Is this correct?



 Tue, 14 Oct 2003 01:11:40 GMT   
 string matching in ksh
/ expanded by the shell.  The equivalent of .* in shell is plain * and
/ there is no equivalent to ^ for indicating the start of a line.

I couldn't find a way to RE checking in bash so I did

    $(echo $string | grep $pattern | wc -l) -eq 1

--
Bush and{*filter*} Bait: Robin Red {*filter*}, Blue Tit, Jackass
Penguin, Erect-crested Penguin, Red-necked Grebe, Fairy
Prion, Rock Shag. Machine censorred for you protection.
___________________/\_________Elect LUM World Dictator!
:)-free zone.      \/     http://www.**-**.com/ ~wyrmwif/
Enjoy as California falls into the ocean and takes rest
of the world with it!            She who laughs last...



 Tue, 14 Oct 2003 11:41:12 GMT   
 string matching in ksh
mlinda...@hotmail.com (Another Ded Dzhen) writes:

What do you need the command substitution and wc for?
If you simply want to test it, you can do simply

if echo "$string" | grep "$pattern" >/dev/null
then
  ...

If your grep supports -q option you can use it instead
of the redirection.

One problem with either is that echo may react to special
characters in the string. Double-quoting it helps a little,
but backslashes or leading dashes can still cause problems
(very system-dependent); printf "%s\n" "$string" is safer.
The pattern also needs to be quoted.

If the pattern is anchored in the beginning (begins with ^)
and you don't need grep flags like -i, expr can be used instead:

if expr "$string" : "$pattern" >/dev/null
then
  ...

Just remember to drop the ^ from the pattern.

--
Tapani Tarvainen



 Tue, 14 Oct 2003 13:51:30 GMT   
 string matching in ksh
"Allen M. Cohen" <a...@to.gd-es.com> wrote

Yes, it's incorrect, must be space before and after a single '='.

No, using '==' is wrong.  (perl and awk have it, but not ksh :)

BTW, the pattern it refers to is a glob pattern, not a regular expression
pattern.  Reading your very first posting makes me think this
misunderstanding is the foundation of your entire problem.

No no, globbing is not the same as regex expressions, totally different.
Omit the word "regex" from above, or (sigh) maybe admit that the pattern is
kinda-sorta-almost-similar to a "regex", and I might think you understand
the difference.  An example: The term "globbing" is what happens to the *
when you type 'echo *' at the shell prompt.  The * in 'echo *' is a glob
pattern, not a regular expression.  Same as in the [[ ]] expressions, and
case statements: they use glob patterns, not regular expressions.

As the other poster said, use "[[" and "]]" and you don't need to use double
quotes, and then you can specify glob patterns or strings (but not regular
expressions!)

This works,

  [[ abc = a* ]] && print found

This does not,

  [[ abc = "a*" ]] && print found

filename globbing is not performed inside [[ ]] expression, a* does not
expand to all files beginning with a, it is used as a pattern (unless the
string is quoted).

In the 2nd example, the quoted "a*" says the 2 character string a*, and it
is not treated as pattern at all, since the double quotes prevent that.  The
quotes force ksh to treat it as a string comparison, not a pattern
comparison.

If you're thinking you need to quote a* to prevent ksh from expanding a* to
all filenames starting with a, that is wrong inside [[ ]], but correct
elsewhere.  You would quote the a* only to mean the 2-character string a*,
rather than the pattern a*.

Yes.

--
David Thompson
david_a_thomp...@bigfoot.com
Foster City, CA USA



 Fri, 17 Oct 2003 00:24:06 GMT   
 string matching in ksh
In article <GeXG6.31586$Jh5.26931...@news1.rdc1.sfba.home.com>,
        "David Thompson" <david_a_thomp...@bigfoot.com> writes:

ksh93 has '==' !

--
Lothar Paltins                                      l...@tesionmail.de



 Fri, 17 Oct 2003 05:21:53 GMT   
 string matching in ksh
/ > No, using '==' is wrong.  (perl and awk have it, but not ksh :)
/
/ ksh93 has '==' !

And now do you wonder why I just use $(echo|grep) which has worked in this
form for years, and previously would have worked as `echo|grep` since the
dark days before SVID?

--
Bush and{*filter*} Bait: Robin Red {*filter*}, Blue Tit, Jackass
Penguin, Erect-crested Penguin, Red-necked Grebe, Fairy
Prion, Rock Shag. Machine censorred for you protection.
___________________/\_________Elect LUM World Dictator!
:)-free zone.      \/     http://www.**-**.com/ ~wyrmwif/
Enjoy as California falls into the ocean and takes rest
of the world with it!            She who laughs last...



 Fri, 17 Oct 2003 09:01:10 GMT   
 string matching in ksh
"Lothar Paltins" <l...@tesionmail.de> wrote

'Bout time ... :)

Anyways, Solaris only has ksh88, not ksh93, at least
from what I can tell ... <Esc><Ctrl-V> says this:

Version M-11/16/88i

Gee, what system was the original poster talking about,
anyways?  I did a quick look-see but never could found it,
so I spoke what what I know to be generally true.  Besides,
my Solaris man page has the same error WRT ==, so I presumed
he's using ksh88 on Solaris... could be wrong...

--
David Thompson
david_a_thomp...@bigfoot.com
Foster City, CA USA



 Fri, 17 Oct 2003 11:55:33 GMT   
 string matching in ksh

Solaris /bin/ksh (Version M-11/16/88i) has "==".



 Fri, 17 Oct 2003 12:53:47 GMT   
 string matching in ksh

...See also the diagnostics of the tracing switch "-x", BTW.

And Solaris2.6+ has dtksh (Version M-12/28/93d)



 Fri, 17 Oct 2003 23:42:53 GMT   
 string matching in ksh
"Michael Wang" <mw...@mindspring.com> wrote

Hmmm, the '==' is not documented in the Solaris man page,

  man ksh | col -bx | grep -e '=='

Ok, does this work?

  [[ hello == hello ]] && print hello
  hello

..*blush*.., uh, yes, it does.  And patterns?

  [[ hello == hell* ]] && print hello
  hello

Yep, == works!  I'll be darndarn.  Well, after I get
this eggegg off my faceface, I'm off to the pubpub.

:-)

--
David Thompson
david_a_thomp...@bigfoot.com
Foster City, CA USA



 Sat, 18 Oct 2003 00:42:31 GMT   
 
   [ 14 post ] 

Similar Threads

1. ksh conditional (string match pattern)

2. Urgent for KSH Gurus : matching source and target lists to create new one(ksh)

3. netfilter string match

4. String matching algorythm

5. Matching strings with regular expressions

6. Unix - Matching data string from 2 files

7. Append string to each line if it matches a pattern

8. Search for best matched portion of a string

9. XPosting: Search for best matched string

10. rm Non-Matching string


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software