On Mon, Jun 08, 2009 at 10:56:09AM +0100, Tom Brown wrote:
echo foo.bar.VALUE.baz.lala | awk -F. '{ print $(NF-2); }'
excellent - just what i needed
awk is probably the most readable way. In traditional shell stuff like this used to be done in awk or sed awk -F. '{print $(NF-2)}' sed -n 's/^.*.([^.]*).[^.]*.[^.]*$/\1/p'
Now you _can_ do it totally inside a modern shell in a variety of ways. Here are three options (tested with ksh93; _should_ work in bash, but not tested)
1) Use IFS to split the string
a=foo.var.VALUE.baz.lala OIFS="$IFS" IFS="." set -- $a IFS="$OIFS" shift $#-3 echo $1
2) variation using arrays
a=foo.var.VALUE.baz.lala OIFS="$IFS" IFS="." set -A A -- $a IFS="$OIFS" let x=${#A[*]}-3 echo ${A[$x]}
3) Using string pattern matching
a=foo.var.VALUE.baz.lala front=${a%.*.*.*} b=${a#$front.} b=${b%%.*} echo $b