1

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?

2

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
  • I tried your command for some reason echo ${prvar[0]} only prints cycHo and then I tried echo ${prvar[@]} ${#prvar[@]} prints cycHo tAdapterNumber cycHo tAdapterID cycManagerID 5 . Because I am not inputing it into prvar with " " s is not being printed. – SamFlynn Jun 30 '15 at 7:04
  • 1
    @SamFlynn what does od -c <<<"$IFS" say? – muru Jun 30 '15 at 7:06
  • 0000000 : % s \n 0000005 – SamFlynn Jun 30 '15 at 7:07
  • And why is your IFS set to %s\n? – muru Jun 30 '15 at 7:07
  • Yeah I has set it to that before, when I was working on something else. Changed it to IFS=",". Okay now on echo ${prvar[0]} this is printed cycHostAdapterNumber cycHostAdapterID cycManagerID – SamFlynn Jun 30 '15 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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