-1

I am to copy "SNMP Agent is up" and "::=101" and count the number of variables present (example here there are 5) and put them in 3 Different variables.

FROM  FILE.TXT
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        

What will remain constant in all other files

DESCRIPTION "  ";   
::= xxx(number);      
VARIABLES { variable1, variable2}

Any commands SED, GREP, AWK or anything else that I can use here take these out from the file.txt

What I need is that I find DESCRIPTION and copy the text that comes after it. The same for the number I find ::= and copy text after it. For variable I find VARIABLE and find the number of variables after it.

So assume these variables
desc is variable for DESCRIPTION
numvar is total number of VARIABLES
casenum is the number that comes after ::=

So when I print these above variables then

OUTPUT for
desc variable

SNMP agent is up.

OUTPUT for
numvar variable

5

OUTPUT for
casenum variable

101

EDIT
FOR NO VARIABLES

sCSISmart2 TRAP-TYPE
ENTERPRISE cyclone
DESCRIPTION
"SNMP Agent is down."
--#TYPE "SNMP Agent is down."
--#SUMMARY "SNMP agent is down"
--#ARGUMENTS {}
--#SEVERITY INFORMATIONAL
--#TIMEINDEX 100
--#STATE OPERATIONAL
--#HELP "scsismrt.hlp"
--#HELPTAG 102
::= 102

FOR ONE VARIABLE

sCSISmart6 TRAP-TYPE
ENTERPRISE cyclone
VARIABLES {cycHostAdapterNumber}
DESCRIPTION
"The HostAdapter# %d has Failed"
--#TYPE "Host adapter failed"
--#SUMMARY "The HostAdapter# %d has Failed"
--#ARGUMENTS {0}
--#SEVERITY CRITICAL
--#TIMEINDEX 100
--#STATE OPERATIONAL
--#HELP "scsismrt.hlp"
--#HELPTAG 110
::= 110

  • I would like to know the reason why I have been downvoted. If their is some problem with my questioning I would like to know so that it never happens again – SamFlynn Jun 15 '15 at 8:57
  • I didn't, but... Is this something you need to do on a regular base or incidentally? It would be easy to write a small script that does the job with the file as argument, but that might be a bit overdone for a single-time usage. – Jacob Vlijm Jun 15 '15 at 9:02
  • @SamFlynn There is some downvoter on questions today. Do not worry. I did not downvote, but vote to close as off-topic. – Pilot6 Jun 15 '15 at 9:05
  • @Jacob Vlijm No, I have to do the same thing for around 237 files. – SamFlynn Jun 15 '15 at 9:07
  • 1
    Ok, I tested them both and they both work flawless. I updated mine to also match whitespaces which where not present the last time I checked the sample, now if there are they will be stripped before being printed – kos Jun 15 '15 at 12:25
2

Possibly it can be done way better. However it can be done using these 3 commands:

desc="$(< inputfile awk '/DESCRIPTION/ {getline; sub(/^\s*"/, ""); sub(/"\s*$/, ""); print}')"
numvar="$(< inputfile awk '/VARIABLES/,/DESCRIPTION/ {count++}; END {if (count>0) print count-1; else print 0}')"
casenum="$(< inputfile awk '/::=/ {sub(/^\s*::=\s*/, ""); sub(/\s*$/, ""); print}')"
  • What i need is that I find DESCRIPTION and copy the text that comes after it. The same for the number I find ::= and copy text after it. For variable I find VARIABLE and find the number of variables after it. – SamFlynn Jun 15 '15 at 9:15
  • 1
    @SamFlynn Ok I think I've got it, let me update the answer – kos Jun 15 '15 at 9:20
  • 1
    @SamFlynn See if the update is what you were looking for. – kos Jun 15 '15 at 9:52
  • 1
    @SamFlynn I updated anyway, now it matches exactly the output you described. – kos Jun 15 '15 at 10:18
  • 1
    @SamFlynn I also updated the third command since after the edit there are multiple spaces before the number, now you should be set. – kos Jun 15 '15 at 10:23
2

EDIT:

Create a file, call it whatever you like, and copy the awk script bellow into it. chmod +x scriptname.awk to make it executable.

#!/usr/bin/awk -f

BEGIN {var_num=0} 
/VARIABLES/,/}/ {
    sub("{","");
    sub("}","");
    sub("VARIABLES",""); 
    if ($0~/[[:alpha:]],/||/[[:alpha:]]/)var_num+=1
    }

 /DESCRIPTION/{ 
    getline; printf "%s,",$0
    } 
/::=/ {
    printf $2","
    }
END{ printf var_num }

Now here's my example of running the script:

$ IFS=","; ARRAY=($(./three-lines.awk test-text.txt  | xargs echo ))                               
$ echo ${ARRAY[0]}; echo ${ARRAY[1]}; echo ${ARRAY[2]}                                             
SNMP Agent is up. 
101
5

Effectivelly, this creates an array of 3 elements, and those elements are what awk script extracts form a file. Regex [[:alpha:]] is used to consider alphabetical scrings and alphabetical strings with comas as variables, hence var_num gets incremented; if there's no lines that match that pattern, var_num is not incremented ORIGINAL

Here's my awk one-liner:

awk 'BEGIN {var_num=0} /VARIABLES/,/}/ {sub("{","");sub("}","");sub("VARIABLES",""); var_num+=1} END{ print var_num}' test-text.txt

Basic idea: create variable var_num, tell awk to read everything between lines containing VARIABLES string and curly brace }. Get rid of that string, all the curly braces, and we only have strings that represent variables. Add 1 for each line to var_num. In the end - print the total of counted variables.

The reason for deleting VARIABLES and curly braces, is that we don't want to count lines that contain only VARIABLE string or the curly brace. So we need to make sure they're gone.

Another pattern that I've noticed is that each variable is coma-separated. Thus you could use an if statement , something like if ($0~/,/) { var_num+=1 }

  • See this comment (and the updated question); I understood the same at first, but the question changed dramatically (and it's way more harsh now) – kos Jun 15 '15 at 9:38
  • 1
    @SamFlynn I think the issue is with " "The HostAd" , there's an extra double quote there – Sergiy Kolodyazhnyy Jun 15 '15 at 10:43
  • 1
    @SamFlynn no idea what you're talking about with TRAPS, but . . . . keep on rock-n-rolling, right ? :D Mean while, I'm gonna edit my answer a little. Fixing a small bug – Sergiy Kolodyazhnyy Jun 15 '15 at 10:56
  • 1
    As soon as I get back home, I'll edit my answer. I'm out of town so far. I've looked through the examples. As I understand you're gonna put variables all on one line. Is that right ? – Sergiy Kolodyazhnyy Jun 17 '15 at 12:18
  • 1
    @SamFlynn OK, I'll work on this. Will let you know once I'm done – Sergiy Kolodyazhnyy Jun 22 '15 at 14:20

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.