[CentOS] script help - '3rd last field'

William L. Maltby CentOS4Bill at triad.rr.com
Mon Jun 8 12:01:32 UTC 2009


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




More information about the CentOS mailing list