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.

2
  • Call me stupid, but I don't see any ”inverted commas” at all in the output. Commented Mar 27, 2018 at 16:22
  • I believe “inverted commas” is another term for single or double quotes, @maulinglawns — en.m.wiktionary.org/wiki/inverted_comma Commented Mar 27, 2018 at 16:24

1 Answer 1

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 
6
  • 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 . . . Commented Mar 28, 2018 at 12:36
  • @PaulDavies ah, I don't know Unix (yet!). How about 2nd proposed solution, using cut and tr? Does it work? Commented Mar 28, 2018 at 16:06
  • @PaulDavies check out 3rd solution I've added. Hope it works for you! Commented Mar 28, 2018 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 Commented Mar 28, 2018 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). Commented Mar 29, 2018 at 9:00

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.