Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ksh93

Status
Not open for further replies.

jameschaucer

Programmer
Dec 29, 2016
4
US
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



 
Here's one way...

Code:
#!/bin/ksh

LINE="abc(hij)=lmn"

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

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

[indent]print "V1=${V1}\nV2=${V2}\nV3=${V3}"[/indent]

fi

 
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.


 
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.
 
Hi

jameschaucer said:
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:
[teal][[[/teal] [navy]$line[/navy] [teal]=~[/teal] [purple]([a-z]+)\ \(([a-z]+)\)\ =\ ([a-z]+)[/purple] [teal]]][/teal]

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

Feherke.
feherke.ga
 
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!
 
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}"

 
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.
 
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"


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top