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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

want to use substr on a variable in korn script with awk 5

Status
Not open for further replies.

jcchoy

Programmer
Feb 1, 2003
9
CA
Hello. I am really frustrated by this. I am trying to put a simple substr function into a Korn shell script to get the year into a korn shell variable. My problem is there is no substr function in korn shell script. And awk substr function only takes input files ! I looked into piping but not sure how to get my result back to a script variable. Here is my latest code in my korn script :

hosp = hosp92
hospno = ' '

echo $hosp | awk '{substr($1,5,2)}' > $hospno

Don't know if I can pipe to a variable or not. In rexx, or perl, it is so simple, but trying to substr in korn shell script, I need more examples on awk that work on $variables vs. from input files.

Can someone help me ?
 
echo $hosp | awk '{ print substr($1,5,2)}' > $hospno

expr $hosp 5 2 > $hospno


regards Gregor
Gregor.Weertman@mailcity.com
 
I think this is what you want:
[tt]hospno=$(echo $hosp | awk '{ print substr($1,5,2)}')[/tt] //Daniel
 
The easiest way to get '92' into hospno is this:

hosp=hosp92
hospno=${hosp##hosp}

no calls to other commands required.

-jim
 
Thank you so very much for all who responded so quickly to my plea for help. I am almost there except I missed the print in awk and I can redirect to shell $variable which is nice. Jim's response is really the neatest although I don't mind learning all the possible ways to accomplish the same thing. This has been a very good lesson on unix shell scripts for me.
 
You can do this in Korn Shell. If the year part is always the last two digits and there is an alphabetic character just before it (as in your example), you can do the following...
[tt]
HOSP=hosp92
HOSPNO=${HOSP##*[A-z]}
[/tt]
The construct of [tt]${variable##pattern}[/tt] means to return the contents of [tt]variable[/tt], minus the largest part matched by [tt]pattern[/tt]. So the example I gave effectively removed everything but the right most numeric digits.

Other forms are...
[tt]# [/tt]removes the smallest pattern from the left.
[tt]## [/tt]removes the largest pattern from the left.
[tt]% [/tt]removes the smallest pattern from the right.
[tt]%% [/tt]removes the largest pattern from the right.

This is good for things like removing file extensions ([tt]${FNAME%.*}[/tt]) and such.

Hope this helps.
 
Another way, if you know the year is always the last two characters, you can do the following...
[tt]
typeset -R2 HOSPNO
HOSP=hosp92
HOSPNO=${HOSP}
print $HOSPNO
92
[/tt]
The [tt]typeset[/tt] command forces a variable to act a certain way. The "[tt]-R2[/tt]" means that the variable will be right justified and only two characters wide. That means ANYTHING you assign to it, this variable will only be set to the right two most characters.
 
Here's another way...

year=`echo "hosp92" | sed 's/.*\([0-9][0-9]\)/\1/'`
unsure if the last "/" is needed.
 
or even

HOSP=hosp92
year=`echo "$HOSP" | tr -d [:alpha:]`
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top