It is currently Thu, 24 Jul 2014 14:24:59 GMT



 
Author Message
 How to get a substring in ksh
I need to extract the last two digits of a string. For example: Day23 - get 23
back, Mon2Year45 - get 45 back.

I can not find anything useful in ksh man page. I am really not an expert with
shell scripts either. Anyone could help? Really appreciate.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.**-**.com/ ;     Search, Read, Discuss, or Start Your Own    



 Tue, 03 Jul 2001 03:00:00 GMT   
 How to get a substring in ksh
In article <77nmnt$bs...@nnrp1.dejanews.com>,

Where is the original string?  In a file, in an environment variable?

echo Day23 | sed -e "s/.*\(..\)/\1/"

yields 23 (but doesn't care whether the last two characters of the
string are numeric).

HTH,
Hunter
--
J. Hunter Johnson        | "A little consistent wholesome modeling and
jhunt...@lexis-nexis.com |   costly servanthood are worth millions of
(937) 865-6800 x5385     |    true words harshly spoken."
Lexis-Nexis, Dayton, OH  |                               -- Ron Sider



 Tue, 03 Jul 2001 03:00:00 GMT   
 How to get a substring in ksh

Or if the value is contained in a variable:

echo ${VARNAME##*[a-zA-Z]}

This will strip off all but numbers at the end, no matter how many
numbers, as long as it ends with a number.



 Tue, 03 Jul 2001 03:00:00 GMT   
 How to get a substring in ksh
In bourne shell I would use something like:
DATE="Mon2YEar45"
LAST=`echo $DATE | awk '{ print substr($1,length($1)-1,2) }'`

This only works when length of string >= 2 chars, otherwise you
have to expand the awk script for error check.

Kornshell should be compatible with bourne shell so it
should work there too.

Succes..



 Fri, 06 Jul 2001 03:00:00 GMT   
 How to get a substring in ksh

Ksh and bash will work with:  echo ${DATE##[A-Za-z]}

This does remove any part of the string up to the last letter. It does
not check of the length of the remaining string.

Regards,
--
   @..@    Marti Rijken  <mrij...@natrix.demon.nl>
  (----)   <URL:http://www.natrix.demon.nl/>
 ( }__{ )  Linux 2.0.36 with RedHat 5.2



 Sat, 07 Jul 2001 03:00:00 GMT   
 How to get a substring in ksh
In article <slrn7a9kdl.t1.mrij...@natrix.demon.nl>,

in ksh,  typeset can be used to extract the first/last n chars of
a variable:

$ typeset -L3 left3
$ typeset -R2 right2
$ left3=Day23
$ right2=Day23
$ print "left3=$left3 right2=$right2"
left3=Day right2=23

Dan Mercer

damer...@mmm.com

Opinions expressed herein are my own and may not represent those of my employer.



 Sat, 07 Jul 2001 03:00:00 GMT   
 How to get a substring in ksh

I think you forget to type "*" before [A-Za-z].

--
unix programs: niftp (non-interactive recursive ftp), hide (hide command args),
submit (replace nohup), etc from ftp://ftp.mindspring.com/users/mwang/unix-prog
Michael Wang, mw...@ml.com, Merrill {*filter*}, World Financial Center, 212-449-4414



 Sun, 08 Jul 2001 03:00:00 GMT   
 
   [ 7 post ] 

Similar Threads

1. Substring function/command in ksh

2. check substring in ksh

3. Getting largest files in ksh.

4. KSH: Help sought getting wrapper code to work in set-uid mode

5. Getting hostname is Xterm ToolBar with respect KSH ???

6. best practice for getting exit status of pipeline commands (bash, ksh, etc)

7. getting a pid in ksh

8. Division in ksh(getting the exact number) Please HELP

9. ksh command history gets full !!???

10. Ksh: problem getting conditional to work with !


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software