Hello,
how can i extract the 3 last characters of a string ?
Thanks
--
Jean-Paul Saliou
Alcatel Telecom - Brest
Tel : 02.98.14.31.82
Thanks
--
Jean-Paul Saliou
Alcatel Telecom - Brest
Tel : 02.98.14.31.82
It isn't possible with standard bourne shell.Quote:>Hello,
>how can i extract the 3 last characters of a string ?
>Jean-Paul Saliou
With the posix shell it is possible. In the posix shell there are
a couple of string addressing mechanisms that deal with arbitrary
chunks of a given string. Refer to the ${VAR%pattern) part of your
local man page.
so, with a posix shell, you could get the last three letters of a
variable by something like:
---
read var
until case "$var" in ???|??|?|'') ;; *) false ;; esac
do
var="${var#?}"
done
echo "$var"
---
(get variable. start loop where we check to see if it is 3 or fewer
characters, if not, chop off first character and check again. Then
print the variable.)
Unlike the [[ ]] and extended metacharacters, these structures
are available in any shell that pretends to be posix compliant (bash,
ash, pdksh, probably zsh).
There is probably a shorter way to do this using sed, awk, perl,
C, fortran, ed, emacs, bliss, and perhaps even cut.
chris
One method is with expr(1)
last=`expr "$instr" : '.*\(...\)$'`
The value after the colon operator is a regular expression, which can
allow one grouping (parens). If a grouping is not included, the number
of characters matched is printed to stdout. If a grouping is included,
the matched substring is printed. In either case, the return code is
zero (0) if there was a match, one (1) otherwise.
The regular expression is:
.* - match any number of characters
\( - start a regular expression grouping
... - (three dots) three of any character
\) - end the grouping
$ - match the end-of-string
expr(1) implicitly matches from the beginning of the string, so you must
have the initial ".*".
I hope this helps.
-Arcege
> Hello,
> how can i extract the 3 last characters of a string ?
> Thanks
> --
> Jean-Paul Saliou
> Alcatel Telecom - Brest
> Tel : 02.98.14.31.82
If you're using ksh, bash, POSIX shell etc, try using:Quote:>how can i extract the 3 last characters of a string ?
typeset -R3 fred=${jim}
Though, be warned; in a function this will make a local variable so it
won't be publically available outside the function, eg:
function foo
{
typeset fred=jim
fooQuote:}
dave
--
"Dear God above (if you exist), Hope you see the funny side to this,
Now don't get cross - don't bite your nails,
Oh, Son of Man your mission's failed."
- Skyclad "The Sinful Ensemble"
> Thanks
> --
> Jean-Paul Saliou
> Alcatel Telecom - Brest
> Tel : 02.98.14.31.82
>If you're using ksh, bash, POSIX shell etc, try using:
>typeset -R3 fred=${jim}
Beware -- the space after the `:' is required, to avoid confusion with
the ${foo:-bar} parameter expansion.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
( ``Discere est Dolere'' -- chet)
--
> >>how can i extract the 3 last characters of a string ?
> >If you're using ksh, bash, POSIX shell etc, try using:
> >typeset -R3 fred=${jim}
> POSIX shells are not required to support "typeset". Those that do
> are probably just ksh in disguise.
Only one problem with this. If the string is shorter than 3 characters this
statement returns nothing. Maybe You want the the whole string to be returned
in these cases.
2. Linux won't see my SB16. :-(
3. Execute a sh script under perl and sh: sh script; perl script?
5. root's shell, /sbin/sh, /bin/sh and /usr/ksh
6. KDE 2.1, missing libXpm.so now on FTP server ..
7. #!/bin/sh #!/usr/bin/sh can I do both for 2 diff machines
8. smount
9. ASCII 128-255 in sh script: sh strips bit 7!!!
10. sh var -> awk var -> sh var
11. sh Xinstall.sh -check error
12. Any difference between /sbin/sh and /bin/sh?
13. Making a cd with makecdfs.sh burncd.sh