Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm not a BASH and/or grep/AWK expert so hoping somebody can quickly help me with this simple query. Consider the following string:

XenCenter.CustomFields.autostart: 120; last_shutdown_time: 20120418T11:24:32Z; last_shutdown_action: Destroy; last_shutdown_initiator: external; last_shutdown_reason: halted; import_task: OpaqueRef:1168d51a-e1ab-b02a-0db7-e6f9bd8fd269; mac_seed: 778079d8-5917-c5ce-4800-ee4321f3ed70

How do I extract the "120" bit above on the "autostart: 120" sub-string?

NOTE: the position and/or order of "XenCenter.CustomFields.autostart: 120" in the above string could and will change so can't rely upon it being the nth word

share|improve this question

5 Answers 5

If you don't know for certain that the fields will always be in the order shown, something like this should work:

# assuming the line is stored in $x as others have done
echo $x | sed -e 's/; /\n/' | grep 'autostart:' | cut -d ' ' -f 2
share|improve this answer

Or to do it in one step:

$ echo $x | awk -F'[ ;]' '{print $2}'
share|improve this answer
    
I failed to add the caveat I've just added about order/position –  Rob Nicholson Apr 19 '12 at 8:58

assuming

var="XenCenter.CustomFields.autostart: 120; last_shutdown_time: 20120418T11:24:32Z; last_shutdown_action: Destroy; last_shutdown_initiator: external; last_shutdown_reason: halted; import_task: OpaqueRef:1168d51a-e1ab-b02a-0db7-e6f9bd8fd269; mac_seed: 778079d8-5917-c5ce-4800-ee4321f3ed70"

you can extract 120 by

echo $var | nawk -F"[:; ]" '{print $3}'

if you need to assign tat value to a var for further processing, just type this in your script

VALUE=$(echo $var | nawk -F"[:; ]" '{print $3}')

and 120 will be stored into $VALUE

share|improve this answer
    
nawk is by no means universal, and may be unwise to use unless it also fully implements POSIX awk. –  adaptr Apr 19 '12 at 9:16
    
agree, the command works with awk –  m0ntassar Apr 21 '12 at 21:41

AWK supports custom record and field seperators (default are newline and whitespace respectably).

When I look at your string it is a combination of variables separted by ; and fields sperated by :

The following awk command sets these seperators (RS = record seperator, FS = field seperator) and checks if field 1 contains (the regex) "autostart" and prints the second field of the record:

awk 'BEGIN { RS=";" ; FS=": " } $1 ~ /autostart/ { print $2 }'

In a shell this produces:

$ echo $STRING | awk 'BEGIN { RS=";" ; FS=": " } $1 ~ /autostart/ { print $2 }'
120
share|improve this answer
    
This looks like the ticket - I knew AWK could be used for string parsing like this as I'd seen it in other scripts. Will try this later –  Rob Nicholson Apr 19 '12 at 8:55

If you have this string stored in variable x, you can simply do this:

$ echo $x | awk {'print $2'}
120;

To eliminate the semicolon, you can use cut like:

$ echo $x | awk {'print $2'} | cut -f1 -d \;
120

This remains valid while you have the 120 separated by space from previous string and it is in the 2nd position (field).

share|improve this answer
    
I should have clarified that the position and/or order of items in the string can and will change –  Rob Nicholson Apr 19 '12 at 8:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.