×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

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

ksh93

ksh93

(OP)
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
v1=${.sh.match[1]}
v2=${.sh.match[2]}
v3=${.sh.match[3]}
print "v1=$v1 \nv2=$v2\nv3=$v3"
fi


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.

thx



RE: ksh93

Here's one way...

CODE

#!/bin/ksh

LINE="abc(hij)=lmn"

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

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

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

(OP)
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

Hi

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]+) ]] 

Feherke.
feherke.ga

RE: ksh93

(OP)
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...

CODE

#!/bin/ksh

LINE="abc(hij)=lmn"

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

print $LINE | read V1 V2 V3

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

RE: ksh93

(OP)
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...

CODE

#!/bin/ksh

line="abc(hij)=lmn"

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