Ubuntu – Storing some strings in a array

bashcommand linetext processing

I have this

sCSISmart1  TRAP-TYPE   
    ENTERPRISE  cyclone                        
    VARIABLES {cycHostAdapterNumber, cycHostAdapterID, cycManagerID}          
    DESCRIPTION                             
    "SNMP Agent is up."                       
    --#TYPE "SNMP Agent is up."                    
    --#SUMMARY "SNMP Agent is up"                    
    --#ARGUMENTS {}                           
    --#SEVERITY INFORMATIONAL                       
    --#TIMEINDEX 100                  
    --#STATE OPERATIONAL                    
    --#HELP "scsismrt.hlp"                          
    --#HELPTAG 101                    
::=  101      

What I want to do

Some command to copy each name of VARIABLE into a array named var

So output of above

echo "${var[0]}"
cycHostAdapterNumber

echo "${var[1]}"
cycHostAdapterID

echo "${var[2]}"
cycManagerID

Another example

sCSISmart1  TRAP-TYPE   
    ENTERPRISE  cyclone                        
    VARIABLES  { scellNameDateTime,                      
                     scellSWComponent,                     
                     scellECode,                  
                     scellCAC,                     
                     scellEIP}                           
    DESCRIPTION                             
    "SNMP Agent is up."                       
    --#TYPE "SNMP Agent is up."                    
    --#SUMMARY "SNMP Agent is up"                    
    --#ARGUMENTS {}                           
    --#SEVERITY INFORMATIONAL                       
    --#TIMEINDEX 100                  
    --#STATE OPERATIONAL                    
    --#HELP "scsismrt.hlp"                          
    --#HELPTAG 101                    
::=  101      

Output for the above

echo "${var[0]}"
scellNameDateTime

echo "${var[1]}"
scellSWComponent

echo "${var[2]}"
scellECode

echo "${var[3]}"
scellCAC

echo "${var[4]}"
scellEIP

Another example

sCSISmart1  TRAP-TYPE   
    ENTERPRISE  cyclone                        
    VARIABLES  {                    
                     scellEIP
               }                           
    DESCRIPTION                             
    "SNMP Agent is up."                       
    --#TYPE "SNMP Agent is up."                    
    --#SUMMARY "SNMP Agent is up"                    
    --#ARGUMENTS {}                           
    --#SEVERITY INFORMATIONAL                       
    --#TIMEINDEX 100                  
    --#STATE OPERATIONAL                    
    --#HELP "scsismrt.hlp"                          
    --#HELPTAG 101                    
::=  101      

Output for the above

echo "${var[0]}"
scellEIP

What I have tried (All this does is store everything into prvar)

prvar="$(awk '/VARIABLES/,/}/ {gsub("VARIABLES"," "); gsub("}"," "); gsub("{"," ");gsub(","," ");print}' temp1)"

Any ideas how can I achieve this?

Best Answer

If you're sure the names won't have spaces in them, remove the quotes and use ( ) around the command substitution:

prvar=($(awk ... temp1))

The effect:

$ prvar=( $(awk '/VARIABLES/,/}/ {gsub("VARIABLES"," "); gsub("}"," "); gsub("{"," ");gsub(","," ");print}' temp1) )
$ echo ${prvar[@]} ${#prvar[@]}
cycHostAdapterNumber cycHostAdapterID cycManagerID 3