Hi
I need some logic to work out a value for me - this value is _always_ the 3rd last field in a string seperated by '.' but the string could be 5 or 6 fields long, e.g
foo.bar.VALUE.baz.lala
foor.bar.gigi.VALUE.baz.lala
I need to find VALUE - if this were python or something i could do it but this has to be in shell -
Any clues?
thanks
On Mon, Jun 8, 2009 at 4:29 PM, Tom Browntom@ng23.net wrote:
Hi
I need some logic to work out a value for me - this value is _always_ the 3rd last field in a string seperated by '.' but the string could be 5 or 6 fields long, e.g
foo.bar.VALUE.baz.lala
foor.bar.gigi.VALUE.baz.lala
I need to find VALUE - if this were python or something i could do it but this has to be in shell -
awk -F. {'print $3'} awk -F. {'print $5'} awk -F. {'print $6'}
muhammad panji wrote: ...
awk -F. {'print $3'} awk -F. {'print $5'} awk -F. {'print $6'}
awk -F. {'print $(NF-2)'}
Mogens
On Mon, Jun 8, 2009 at 5:29 AM, Tom Browntom@ng23.net wrote:
Hi
I need some logic to work out a value for me - this value is _always_ the 3rd last field in a string seperated by '.' but the string could be 5 or 6 fields long, e.g
foo.bar.VALUE.baz.lala
foor.bar.gigi.VALUE.baz.lala
I need to find VALUE - if this were python or something i could do it but this has to be in shell -
Any clues?
thanks _______________________________________________ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
I am pretty sure there is a way in awk to figure out how many fields you have, then take the total # of fields -3 each time to get the third last one. Just heading out the door and off hand can't remember how it would be done.
Jacques B.
I am pretty sure there is a way in awk to figure out how many fields you have, then take the total # of fields -3 each time to get the third last one. Just heading out the door and off hand can't remember how it would be done.
i can do it in cheetah templating with
set myloc = $getVar("hostname","")split('.')[-3]
the -3 says the last 3rd field in the string - i need the same so maybe awk can do this
Hi.
echo foo.bar.VALUE.baz.lala | awk -F. '{ print $(NF-2); }'
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
On Mon, 2009-06-08 at 10:29 +0100, Tom Brown wrote:
Hi
I need some logic to work out a value for me - this value is _always_ the 3rd last field in a string seperated by '.' but the string could be 5 or 6 fields long, e.g
foo.bar.VALUE.baz.lala
foor.bar.gigi.VALUE.baz.lala
I need to find VALUE - if this were python or something i could do it but this has to be in shell -
Any clues?
Without trying to make code this early in the A.M., I'll give an algorithm that will work entirely in shell. Then the man page should give the details.
1. Make a subshell, either as a separate file or inline using braces-type stuff 2. Inside that, use the set command to change the field separator to "." 3. Use the set command with the string use wish to parse. This sets each field into $1, $2, ... 4. Alternate algorithm # 1 Use the shell variable $# (IIRC) to see how many you have. Use the shell's math capabilities to calculate the variable number you want Use the shell to generate a command (eval, backslashes, ...) to reference that variable 5. Alternate algorithm # 2 do a while loop until $# = 3 (if it's already <= to 3, next is skipped shift access $1 6. Alternate algorithm # 3 If $# > 3 use shell math ability to calculate how many shifts needed use shift with that number as parameter access $1
thanks
<snip sig stuff>
HTH