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

string manipulation in Unix Shell Scripting

Status
Not open for further replies.

biot023

Programmer
Nov 8, 2001
403
GB
Hallo.
I can't seem to find any reference to ways to manipulate string in a shell script. Possibly there aren't any?
Like maybe cut or concatenate or Left or Right?
Any & all help gratefully received.
Douglas JL

A salesman is a machine for turning coke into obnoxious arrogance.

Common sense is what tells you the world is flat.

 
man expr
If in ksh, pay attention at the parameter substitution section of the man page (% and # specials characters within a ${} substitution).

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Sorry, I'm rather new at all this.
What I specificaly need to do is get the numerical part of a variable.
say:
VAR1="MAN_193"

I would want VAR2 to be the last three characters of VAR1 so that VAR2=193.
Is that possible?
Or am I asking too much of shell scripting?
Thanks again, man,
DJL

A salesman is a machine for turning coke into obnoxious arrogance.

Common sense is what tells you the world is flat.

 
In ksh:
VAR1="MAN_193"
VAR2=${VAR1#*_}
echo "$VAR2"

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Sorry, man - that reported a bad substitution.
Cheers, though.
DJL

A salesman is a machine for turning coke into obnoxious arrogance.

Common sense is what tells you the world is flat.

 
works for me in ksh with copy/paste

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
I'm guessing your not using ksh.
Try this...

VAR1="MAN_193"
VAR2=`echo "$VAR1" | sed 's/^*_\(*\)/\1/'`
echo "$VAR2"

this will print anything after the underscore in the string.
not the most efficient or clean. but works!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top