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!

substring 1

Status
Not open for further replies.

JohnMN

Programmer
Oct 28, 2002
13
0
0
US
I am writing a script in ksh.
The user will input a string in the form: NAME_A. I want to strip off the '_A' part and use 'NAME' in later commands. I will also need to use the 'NAME_A' format, so I can't just have user enter 'NAME'.
'NAME' can be any number of characters, but '_A' will always be the last two characters. (It could be either '_A' or '_B').
Any hints as to how I can perform this surgery? Thanks!

John
 
Hi,

You can use cut command to get the NAME. Example of cut command:

echo NAME_A | cut -d "_" -f1

regards,
feroz
 
#!/bin/ksh

# or if you just want to eliminate the last two characters of the variable name:

##l=$(expr $var : '.*') # length of filename

var=NAME_A
NAME=$(echo $var|cut -c1-$(($(expr $var : '.*')-2)))
echo $NAME
 
#!/bin/ksh

a='NAME_A'

echo "root->[${a%%_*}]"
echo "leaf->[${a##*_}]"


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks to all for your quick suggestions!
In my case, I'll use olded's suggestion for cutting off the last two characters. I forgot that 'NAME' can contain underscore characters, so it's easiest just to cut off the last two characters.

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top