Eric
Eric

Reputation: 27

Regex and sed in sh script not evaluating properly

first post here. Trying to capture just the integer output from an SNMP reply with regex. I've used a regex tester to come up with the correct pattern match but sed refuses to output the result. This is just a primitive fact finding script right now, it'll grow into something more complex but right now this is my stumbling block.

The reply to each line of the snmpget statements are:

IF-MIB::ifInOctets.1001 = Counter32: 692749329
IF-MIB::ifOutOctets.1001 = Counter32: 3119381688

I want to capture just the value after "Counter32: " and the regex (?<=: )(\d+) accomplishes that in the testers I could find online.

#!/bin/sh
SED_IFACES="-e '/(?<=: )(\d+)/g'"
INTERNET_IN=`snmpget -v 2c -c public 123.45.678.9 1.3.6.1.2.1.2.2.1.10.1001` | eval sed $SED_IFACES
INTERNET_OUT=`snmpget -v 2c -c public 123.45.678.9 1.3.6.1.2.1.2.2.1.16.1001` | eval sed $SED_IFACES
echo $INTERNET_IN
echo $INTERNET_OUT

Upvotes: 1

Views: 60

Answers (2)

bkmoney
bkmoney

Reputation: 1256

You can do

sed 's/^.*Counter32: \(.*\)$/\1/'

Which captures the value and prints it out with the \1.

Also note that you are using Perl regular expressions in your example, and sed does not support these. It is also missing the substitution "s/" part.

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 204381

$ cat file
IF-MIB::ifInOctets.1001 = Counter32: 692749329
IF-MIB::ifOutOctets.1001 = Counter32: 3119381688

$ awk '{print $NF}' file
692749329
3119381688

$ sed 's/.* //' < file
692749329
3119381688

Upvotes: 1

Related Questions