0

I've completed an SNMP poll to get different licenses from my routers. I've successfully polled the information; I now want to be able to present it. A snapshot of the output I have is:

SNMPv2-SMI::enterprises.9.9.543.1.2.3.1.3.1.1.1 = STRING: "ipbasek9"  
SNMPv2-SMI::enterprises.9.9.543.1.2.3.1.3.1.1.2 = STRING: "securityk9"  
SNMPv2-SMI::enterprises.9.9.543.1.2.3.1.3.1.2.1 = STRING: "securityk9"  
SNMPv2-SMI::enterprises.9.9.543.1.2.3.1.3.1.2.2 = STRING: "uck9"

I just want to show what is in inverted commas:

  • "ipbasek9"
  • "securityk9"
  • etc.

I have found the regex for it "(.*?)" which will highlight all data in the inverted commas, but what command will actually pull the data from the text? I've tried all sorts of variations of awk, sed, grep but still not having any luck.

1

Solution 1

grep has that option built-in, I use it myself sometimes.
From man grep:

       -o, --only-matching
          Print only the matched (non-empty) parts of a matching line, with each  such  part  on  a  separate
          output line.

Then you could use tr to get rid of unwanted characters:

$ tr -d '"'

Test string:

$ echo 'SMI::enterprises.9.9.543.1.2.3.1.3.1.1.2 = STRING: "securityk9"' | grep -E -o '"(.*?)"' | tr -d '"'

Output:

securityk9

Solution 2

Another way, if number of space-delimited fields is consistent, could be using cut (and tr, so still 2 pipes and I'm sure it can be done in one run):

$ cut -d ' ' -f 4 | tr -d '"'

Test:

$ echo 'SMI::enterprises.9.9.543.1.2.3.1.3.1.1.2 = STRING: "securityk9"' | cut -d ' ' -f 4 | tr -d '"'
securityk9

Solution 3

Using perl should be most universal and portable among all Linux and Unix systems having Perl 5 installed. Pipe your output to:

perl -p -e 's/.*?"(\w+)"/$1/g' -

Example:

$ echo 'SNMPv2-SMI::enterprises.9.9.543.1.2.3.1.3.1.1.1 = STRING: "ipbasek9"' | perl -p -e 's/.*?"(\w+)"/$1/g' -
ipbasek9

Explanation:

-p           iterate over each line of input
-e           execute following code
s/foo/bar/g  substitute 'foo' with 'bar' globally, in entire line
.*?"         match any characters non-greedy, so up to first left-most double quote "
(\w+)        match and capture into $1 any word-characters (alphanumeric, underscore _, plus connector punctuation chars)
$1           substitute with with whatever was captured in first parenthesis 
| improve this answer | |
  • Thanks for your response. I'm doing this on Solaris and am getting the error grep: illegal option -- E grep: illegal option -- o Usage: grep -hblcnsviw pattern file . . . – Paul Davies Mar 28 '18 at 12:36
  • @PaulDavies ah, I don't know Unix (yet!). How about 2nd proposed solution, using cut and tr? Does it work? – yahol Mar 28 '18 at 16:06
  • @PaulDavies check out 3rd solution I've added. Hope it works for you! – yahol Mar 28 '18 at 16:45
  • 2nd solution did work! Thanks so much for your help. Every fourth block pulled it out. cut -d ' ' -f 4, 8, 12,16... 40 Will show output tomorrow – Paul Davies Mar 28 '18 at 22:04
  • @PaulDavies I've made a terrible assumption adding new lines to your snmp output while editing your question! Might I ask, why you don't have each device output in a separate line? That'd make it much easier to parse. What I usually do is: for SWITCH in 10.10.10.{10..30}; do echo "$SWITCH"; snmpwalk -v1 -c switchback "$SWITCH" sysdesc | cut -d ' ' -f 4-9; echo; done - this way, reply from each device is on separate line (with optional echo switch IP and empty line between for better readability). – yahol Mar 29 '18 at 9:00

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.