KSH Pattern Match Deletions

KSH Pattern Match Deletions

Post by Dave Butle » Tue, 01 Oct 2002 02:09:36



I want to pull out the CPU speed from the following string "CPU 1 performs
like a 403Mhz Intel Pentium II". I have discovered the ## and %% shell
pattern deletion operators:

(executed under Korn Shell, # character is the prompt and not a comment
character)

# TEST="CPU 1 performs like a 403Mhz Intel Pentium II"
# print ${TEST##*a\ }
403Mhz Intel Pentium II
# print ${TEST%%Mhz*}
CPU 1 performs like a 403

(When I try to combine the two in order to pull out the "403" by itself, it
fails.)

# print ${${TEST%Mhz*}##*a}
ksh: ${${TEST%Mhz*}##*a}: bad substitution

I tried a few other variations, but could not make it work. I know I can do
this by using two statements, but there is probably an easy way that I am
missing.

I know there are probably alternatives using SED and other utilities, but I
thought this would be the most efficient way since it would not call any
external processes? Is this correct?

Thanks for any help,

Dave

 
 
 

KSH Pattern Match Deletions

Post by Chris F.A. Johnso » Tue, 01 Oct 2002 05:13:41



> I want to pull out the CPU speed from the following string "CPU 1 performs
> like a 403Mhz Intel Pentium II". I have discovered the ## and %% shell
> pattern deletion operators:

> (executed under Korn Shell, # character is the prompt and not a comment
> character)

> # TEST="CPU 1 performs like a 403Mhz Intel Pentium II"
> # print ${TEST##*a\ }
> 403Mhz Intel Pentium II
> # print ${TEST%%Mhz*}
> CPU 1 performs like a 403

> (When I try to combine the two in order to pull out the "403" by itself, it
> fails.)

> # print ${${TEST%Mhz*}##*a}
> ksh: ${${TEST%Mhz*}##*a}: bad substitution

   You must have a variable name after ${.

Quote:> I tried a few other variations, but could not make it work. I know I can do
> this by using two statements, but there is probably an easy way that I am
> missing.

    Since the # and % expansion operators remove the beginning of end
    of the variable's value, it cannot be done in one operation.

   You must do it in two steps:

        TEST=${TEST##*a\ }
        print ${TEST%Mhz*}

Quote:> I know there are probably alternatives using SED and other utilities, but I
> thought this would be the most efficient way since it would not call any
> external processes? Is this correct?

    Even two steps is much more efficient than using an external command.

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

KSH Pattern Match Deletions

Post by Dave Butle » Tue, 01 Oct 2002 09:48:06


Quote:>>You must do it in two steps:

That is what I wanted to know. Thank you!!




> > I want to pull out the CPU speed from the following string "CPU 1
performs
> > like a 403Mhz Intel Pentium II". I have discovered the ## and %% shell
> > pattern deletion operators:

> > (executed under Korn Shell, # character is the prompt and not a comment
> > character)

> > # TEST="CPU 1 performs like a 403Mhz Intel Pentium II"
> > # print ${TEST##*a\ }
> > 403Mhz Intel Pentium II
> > # print ${TEST%%Mhz*}
> > CPU 1 performs like a 403

> > (When I try to combine the two in order to pull out the "403" by itself,
it
> > fails.)

> > # print ${${TEST%Mhz*}##*a}
> > ksh: ${${TEST%Mhz*}##*a}: bad substitution

>    You must have a variable name after ${.

> > I tried a few other variations, but could not make it work. I know I can
do
> > this by using two statements, but there is probably an easy way that I
am
> > missing.

>     Since the # and % expansion operators remove the beginning of end
>     of the variable's value, it cannot be done in one operation.

>    You must do it in two steps:

>         TEST=${TEST##*a\ }
>     print ${TEST%Mhz*}

> > I know there are probably alternatives using SED and other utilities,
but I
> > thought this would be the most efficient way since it would not call any
> > external processes? Is this correct?

>     Even two steps is much more efficient than using an external command.

> --
>     Chris F.A. Johnson                        http://cfaj.freeshell.org
>     ===================================================================
>     My code (if any) in this post is copyright 2002, Chris F.A. Johnson
>     and may be copied under the terms of the GNU General Public License

 
 
 

KSH Pattern Match Deletions

Post by Juergen Hec » Tue, 01 Oct 2002 23:26:44




> > I want to pull out the CPU speed from the following string "CPU 1 performs
> > like a 403Mhz Intel Pentium II". I have discovered the ## and %% shell
> > pattern deletion operators:

> > (executed under Korn Shell, # character is the prompt and not a comment
> > character)

> > # TEST="CPU 1 performs like a 403Mhz Intel Pentium II"
> > # print ${TEST##*a\ }
> > 403Mhz Intel Pentium II
> > # print ${TEST%%Mhz*}
> > CPU 1 performs like a 403

> > (When I try to combine the two in order to pull out the "403" by itself, it
> > fails.)

> > # print ${${TEST%Mhz*}##*a}
> > ksh: ${${TEST%Mhz*}##*a}: bad substitution

>    You must have a variable name after ${.

> > I tried a few other variations, but could not make it work. I know I can do
> > this by using two statements, but there is probably an easy way that I am
> > missing.

>     Since the # and % expansion operators remove the beginning of end
>     of the variable's value, it cannot be done in one operation.

>    You must do it in two steps:

>         TEST=${TEST##*a\ }
>         print ${TEST%Mhz*}

[snip]

or here without expansion operators

speed=$(IFS=' M'; set -- ${TEST};echo $6)

Regards
Juergen

 
 
 

KSH Pattern Match Deletions

Post by Chris F.A. Johnso » Tue, 01 Oct 2002 23:45:52




>>         TEST=${TEST##*a\ }
>>         print ${TEST%Mhz*}

> or here without expansion operators

> speed=$(IFS=' M'; set -- ${TEST};echo $6)

    Good one; though if one doesn't have expansion operators, the
    $(...) form won't be available either. :)

        speed=`IFS=' M'; set -- ${TEST};echo $6`

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

KSH Pattern Match Deletions

Post by Sven Maschec » Wed, 02 Oct 2002 01:23:19




>> speed=$(IFS=' M'; set -- ${TEST};echo $6)
>  speed= `IFS=' M'; set -- ${TEST};echo $6`

"It blows up if IFS contains a single-quote." - Geoff Clare

(the original poster's data looks safe in this concern, though)

 
 
 

KSH Pattern Match Deletions

Post by Sven Maschec » Wed, 02 Oct 2002 01:29:33



> [ complete and utter nonsense ]

Article canceled, i strongly apologize! :)
 
 
 

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. ``SIOCSIFFLAGS: Try again'' !?!

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

4. Need help on a script

5. ksh - checking for filenames matching a pattern

6. How to set prompt before starting csh?

7. ksh pattern matching

8. xwindow problem

9. ksh conditional (string match pattern)

10. ksh discrepancy on pattern matching

11. ksh and pattern matching

12. How can I pattern-match in ksh?

13. my fault about pattern matching in ksh