Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here



In ksh93, I want to match on a string and be able to print out the subpatterns like I do in perl.

Given the following:

line="abc (hij) = lmn"

if [[ $line = @(*[a-z]\(*[a-z]\)=*[a-z]) ]]; then
print "v1=$v1 \nv2=$v2\nv3=$v3"

What would do to emit the different pieces of this string? ie, to get v1=abc v2=hij v3=lmn ?

Nothing I try comes close to working.

I have a bunch of perl I've been told to convert into ksh so this answer would help a lot.


Replies continue below

Recommended for you

RE: ksh93

Here's one way...




if [[ ${LINE} == @(*[a-z]\(*[a-z]\)=*[a-z]) ]]

print ${LINE} | sed 's/[()=]/ /g' | read V1 V2 V3
print "V1=${V1}\nV2=${V2}\nV3=${V3}"

RE: ksh93

Some explanation...

The "sed" command is just turning the parenthesis and equal sign into spaces so the "read" sees the values as separate values.

This could also be done with "awk" if you want just certain values from the line, or you need some conditional logic in there too.

RE: ksh93

Thanks, Sam. Yes, of course you're right. I've used sed and awk for years and they will do the job. I'm trying to learn how this is done in native ksh93 though. All those calls to external programs adds a lot of process overhead if you do enough of them.

So, if anyone knows hows to do this using only ksh93 that would be great.

RE: ksh93


Quote (jameschaucer)

In ksh93, I want to match on a string and be able to print out the subpatterns like I do in perl.
Then use a regular expression like you do in Perl :

CODE --> ksh93 ( fragment )

[[ $line =~ ([a-z]+)\ \(([a-z]+)\)\ =\ ([a-z]+) ]]

# or less restrictive on those spaces :
[[ $line =~ ([a-z]+)\s*\(([a-z]+)\)\s*=\s*([a-z]+) ]] 


RE: ksh93

Gee whiz, that's exactly like Perl. I spent an hour googling on docs and never once found such a simple, clear (and sufficiently complex) example.

Thanks Feherke!

RE: ksh93

This should work and is "pure" ksh...




# Set Inter-Field-Separator characters (IFS)
IFS='()= '

print $LINE | read V1 V2 V3

print "V1=${V1}\nV2=${V2}\nV3=${V3}" 

RE: ksh93

Very true, that will work.

But, my example was a simple one I provided in order to learn how to do the much more complex ones I need.

Thanks for the reply however.

RE: ksh93

Here's using an array...




IFS='()= '

eval set -A v ${line}

print "v[0]=${v[0]}\nv[1]=${v[1]}\nv[2]=${v[2]}\n" 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close