0

I need an one-liner, for the following output:

STRING='SNMPv2-MIB::sysDescr.0 = STRING: test test test, test v3.2.5.90, test'
echo $STRING | awk '{print $8}'
v3.2.5.90,

Desired output is

v3.2.5.90

So I need to remove the comma at the end. I could pipe it again to sed but this means there has to be a "one-command" solution.

Thanks in advance

  • Does , only appears one time in string? – cuonglm Nov 16 '16 at 7:36
  • No multiple times. Whole output: SNMPv2-MIB::sysDescr.0 = STRING: test test test, test v3.2.7.32.a, test – M.S. Nov 16 '16 at 7:41
  • 3
    '{gsub(/\,/,""); print $8}'... – jasonwryan Nov 16 '16 at 7:45
  • @fedorqui oh you´re right. copied the wrong string. fixed it – M.S. Nov 16 '16 at 7:52
  • OK so what is the pattern here? The last block of text before the last comma? – fedorqui Nov 16 '16 at 7:55
0

Try this:

echo $STRING | awk -F', | ' '{print $8}'

Works on my end.

1

This works with grep -version 2.25 on Ubuntu

grep -oP '(?<=, test).*(?=,)' <<< "SNMPv2-MIB::sysDescr.0 = STRING: test test test, test v3.2.7.32.a, test"
  • Probably you need (?<=, test ), that is, a space after test. – fedorqui Nov 16 '16 at 7:59
1

Try ordinary bash (even POSIX/standard sh) string tools:

STRING='SNMPv2-MIB::sysDescr.0 = STRING: test test test, test v3.2.5.90, test'
OUTPUT=${STRING%,*}
printf '%s\n' "${OUTPUT##* }"
0

if you want to always capture the version # ( starts with v), then try this

echo ${STR} | awk '$0~/^v/' RS=", "

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.