Forum Discussion

rbgnow_110245's avatar
rbgnow_110245
Icon for Nimbostratus rankNimbostratus
Jan 16, 2009

Get multiple fields from string

How to get multiple fields from a string variable.

 

 

Example:

 

echo "a.b.c.d.e.f"|cut -d. -f2,4-6

 

b.d.e.f

 

 

trying to find the similar feature using getfield like method.

1 Reply

  • You could use getfield for each field, but I think scan would be more efficient. Here are a few examples:

     
     when RULE_INIT { 
      
        set ip 11.22.33.44 
        scan $ip {%d.%d.%d.%d} a b c d 
        log local0. "$a, $b, $c, $d" 
      
        set str "e f g h" 
        scan $str {%s %s %s %s} a b c d 
        log local0. "$a, $b, $c, $d" 
      
        set str "i 2 k 4" 
        scan $str {%s %s %s %s} a b c d 
        log local0. "$a, $b, $c, $d" 
     } 
     

    And the log output:

    Rule : 11, 22, 33, 44

    Rule : e, f, g, h

    Rule : i, 2, k, 4

    Aaron