Monday, September 29, 2008

Shell String Operations: How to get index of a substring in a string

The following commands returns the index of first occurance of character in a string
expr index $string $character
Please note that you can use a regular expression for character

Example:
stringZ=abcABC123ABCabc
echo `expr index "$stringZ" C`
# Retuns 6
echo `expr index "$stringZ" [c-z]`
# Returns 3

No comments:

Post a Comment