How can I pattern-match in ksh?

How can I pattern-match in ksh?

Post by che.. » Wed, 31 Dec 1997 04:00:00



Can anybody tell me how to do what Perl does by

open FL, "myfile.txt"   #I can do exec 3< myfile.txt in ksh
while (<FL>)            #I can do read -u3 line_buf
{ if (/MyPattern/) {... #How can I match part of a string?

Or, how can make a pattern match in ksh on a given string (match part of the

matches the pattern once. But match on what string? In Perl, you can say
$line_buf =~ /MyPattern/ to force the match on $line_buf. There's no
equivalent of =~ in Korn shell, is there?

Thanks for your help.

Yong

 
 
 

How can I pattern-match in ksh?

Post by Charles Dem » Wed, 31 Dec 1997 04:00:00



>Can anybody tell me how to do what Perl does by

>open FL, "myfile.txt"   #I can do exec 3< myfile.txt in ksh
>while (<FL>)            #I can do read -u3 line_buf
>{ if (/MyPattern/) {... #How can I match part of a string?

>Or, how can make a pattern match in ksh on a given string (match part of the

>matches the pattern once. But match on what string? In Perl, you can say
>$line_buf =~ /MyPattern/ to force the match on $line_buf. There's no
>equivalent of =~ in Korn shell, is there?

>Thanks for your help.

>Yong


Grep, sed, awk   (choose one)

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.


 
 
 

How can I pattern-match in ksh?

Post by Dan A. Merc » Wed, 31 Dec 1997 04:00:00



: >Can anybody tell me how to do what Perl does by
: >
: >open FL, "myfile.txt"   #I can do exec 3< myfile.txt in ksh
: >while (<FL>)            #I can do read -u3 line_buf
: >{ if (/MyPattern/) {... #How can I match part of a string?
: >
: >Or, how can make a pattern match in ksh on a given string (match part of the

: >matches the pattern once. But match on what string? In Perl, you can say
: >$line_buf =~ /MyPattern/ to force the match on $line_buf. There's no
: >equivalent of =~ in Korn shell, is there?
: >
: >Thanks for your help.
: >
: >Yong

: Grep, sed, awk   (choose one)

Only if regular expression matching is required,  in which case
expr should be mentioned.  However,  the Korn shell has a very
powerful set of pattern matching expressions that should not be
overlooked.  

while read line
   do
   if [[ "$line" = pattern ]]; then
      action
   else
      action
   fi
   done < file

For instance
while read line
   do
   if [[ "$line" = *Kansas* ]]; then
       # do something for entries containing the word "Kansas"

This can also be expressed negatively
   if [[ "$line" = !(*Kansas*|*Missouri*|*Arkansas*|*Iowa*) ]]; then

Read up on "File Name Generation" in the manpage for more info.
In many cases,  you can do things with File Name Generation that
wouldn't be achievable with regular expressions.

: Chuck Demas
: Needham, Mass.

: --
:   Eat Healthy    |   _ _   | Nothing would be done at all,

:   Die Anyway     |    v    | That no one could find fault with it.

--
Dan Mercer

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

 
 
 

1. ksh pattern matching when pattern is in a variable

Witness this:


  [[ work = $exclude ]] && print excluded

The print is executed on AIX 433, but not on AIX 5.1 or SunOS 5.9.

Looking at the original AT&T ksh book by David Korn, on pg 156 under
Conditional Commands, in the [[ ... ]] section, it says

"ksh expands the operands(s) for each conditional expression primitive
for command substitution, parameter expansion, and quote removal as
required to evaluate the command."

To me, this means it should be expanding $exclude before it interprets
it as a pattern.  And this is certainaly The Right Thing To Do.  What
am I missing?

John.

2. Printing with 2.2.x kernel

3. How can I pattern-match a given string in ksh?

4. Testing IP spoofing...

5. How to config router for 2 networks

6. Pattern matching and extracting the data which matches the pattern

7. Doku to runtime library ?

8. ksh - checking for filenames matching a pattern

9. KSH Pattern Match Deletions

10. ksh pattern matching

11. ksh conditional (string match pattern)

12. ksh discrepancy on pattern matching