2

I am to copy the number 795, and use it another file. This 795 could be any number and can change from file to file. Everything till the enterprises will remain the same even the ending curly brackets So this

OBJECT IDENTIFIER   ::=   { enterprises 795 }

will be come this in other file

VAR="759"

I want to do it in SED or Grep. Thank you for your help!

3
  • 1
    meta.askubuntu.com/questions/14089/…
    – Tim
    Jun 11, 2015 at 17:41
  • 1
    You have asked many (sort of) similar questions..could you please tell your final goal so that we can help you to achieve that ?
    – heemayl
    Jun 11, 2015 at 18:17
  • Okay will be adding a final question describing the whole scope of what i am trying to achieve. But the biggest problem is that I am not able to find a single source on Google that clearly defines how SED works, every website I have been to just leaves things of here and there unexplained. If i were to find a good source on SED I wouldn't have to ask these questions. Any good suggestions @heemayl ?
    – SamFlynn
    Jun 13, 2015 at 6:26

2 Answers 2

2

Using grep with PCRE (Perl-compatible regular expressions):

grep -Po '.* \K\d+(?= }$)'
  • .* \K will match upto the space before the digits and \K will discard the match

  • \d+ will match one or more digits

  • Zero width postive lookahead (?= }$) will ensure that the match is followed by } at the end.

    $ var="$(grep -Po '.* \K\d+(?= }$)' <<<'OBJECT IDENTIFIER ::= { enterprises 795 }')"
    
    $ echo "$var"
    795
    

In case of any file (where there will be multiple matches) better save the matches as array elements so that you can iterate over them later:

var=( "$(grep -Po '.* \K\d+(?= }$)' file.txt)" )

Using sed :

$ sed -r 's/.* ([0-9]+) }/\1/' <<<'OBJECT IDENTIFIER   ::=   { enterprises 795 }'
795
  • -r will enable use to use extended Regex

  • .* will match upto the space before digits

  • ([0-9]+) will match the digits and put the match in group

  • We have just use the group \1 i.e. the digits as teh replacement pattern.

In case of any file (where there will be multiple matches) better save the matches as array elements so that you can iterate over them later:

var=( "$(sed -nr 's/.* ([0-9]+) }/\1/p' file.txt)" )
4
  • So I see you're feeding an input string here, but how do you do this with an input file ? Jun 11, 2015 at 17:58
  • @Serg just use grep -Po '.* \K\d+(?= }$)' file.txt
    – heemayl
    Jun 11, 2015 at 17:59
  • add that to your answer, so the OP sees it Jun 11, 2015 at 18:01
  • @user2446359 Glad it worked....also check my edits regarding a file....
    – heemayl
    Jun 12, 2015 at 17:41
1

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).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .