grep "OBJECT IDENTIFIER" testfile1.txt | awk '{sub("}","");print $5}'
Explanation: since you've requested grep , I'm using grep to get the string you want. Awk prints the 5th collumn (since in awk by default columns are separated by spaces), which will be 795}
. sub
function merely replaces }
with blank. The result is that you extract the number itself.
Note: awk can do this all by itself without grep by using /SEARCHTERM/
form. Here's the code:
awk ' /OBJECT IDENTIFIER/ {sub("}","");print $5}' testfile1.txt
Notice , that in both code versions, you're relying on finding the string and extracting whatever number is placed between the curly braces, so regardless of what the number is, you still going to extract it as long as it is in there, next to the right string.
Version with grep and cut
A bit more lengthy alternative to the first command is this:
grep "OBJECT IDENTIFIER" testfile1.txt | cut -d '{' -f2 | cut -d'}' -f1 | cut -d' ' -f2
cut
command does what it sounds like - cuts of things after specific delimiter. So here we find the necessary string (output there is OBJECT IDENTIFIER ::= { enterprises 795 }
), cut off whatever comes second after '{' (output is now enterprises 795 }
), cut of whatever comes before '}' (now we have enterprises 795
), and cut of second thing after ' ' (space) (now we get 795
or whatever number is there).