Substring in sh

Substring in sh

Post by Jean-Paul SALI » Sat, 03 Apr 1999 04:00:00



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

 
 
 

Substring in sh

Post by chris ulri » Sat, 03 Apr 1999 04:00:00


Quote:>Hello,
>how can i extract the 3 last characters of a string ?

>Jean-Paul Saliou

  It isn't possible with standard bourne shell.  

  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



 
 
 

Substring in sh

Post by Michael P. Reill » Sat, 03 Apr 1999 04:00:00


: Hello,
: how can i extract the 3 last characters of a string ?

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

 
 
 

Substring in sh

Post by Kurt J. Lanz » Sat, 03 Apr 1999 04:00:00



> Hello,
> how can i extract the 3 last characters of a string ?

'sed' will do it. Some systems have an expanded 'expr' which will
do it. I'm sure you could do it with 'awk'. Start reading man pages.
 
 
 

Substring in sh

Post by Hemant Kalvi » Sat, 03 Apr 1999 04:00:00


$ echo "ABCDEF12:345deasdsdf" | sed        's/.*\(...\)$/\1/'
sdf

> 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


 
 
 

Substring in sh

Post by David Lod » Sun, 04 Apr 1999 04:00:00



Quote:>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}

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

Quote:}

foo
echo ${fred}

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"

 
 
 

Substring in sh

Post by pannee » Fri, 09 Apr 1999 04:00:00


Use typseset -R3 varname=oldvar for last three chars
ans typeset -L3 varname=oldvar forfirst three chars
panneer

> 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


 
 
 

Substring in sh

Post by Chet Ram » Sat, 10 Apr 1999 04:00:00





>>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}

Bash doesn't implement the justification options to typeset.  You can
use
        foo=abcdefgh
        echo ${foo: -3}

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)


 
 
 

Substring in sh

Post by Geoff Cla » Wed, 21 Apr 1999 04:00:00


[ This is a repost of an article that didn't make it beyond the local
  network.  Sorry if the topic is a bit out of date. ]


>>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.

--


 
 
 

Substring in sh

Post by Sreenivas Kothapall » Wed, 21 Apr 1999 04:00:00



> [ This is a repost of an article that didn't make it beyond the local
>   network.  Sorry if the topic is a bit out of date. ]


> >>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.

How about this?
expr $string : '.*\(...\)'