Search code examples
regexgrep

How to grep the pattern within parenthesis?


Here is the string :

Usage:       xp (UUID: 30503c82-bf04-4f75-ab8f-129b8b350487)

I want to grep this pattern

30503c82-bf04-4f75-ab8f-129b8b350487

I can use grep and sed to pick off it,using like this:

grep \(.*\) -o | sed 's/[()]//g'

can i use only grep to accomplish this opration?


Solution

  • You can use

    egrep 'UUID[^\)]+' -o
    

    which will include the "UUID:" prefix that your example code produces.

    To get only the id, you can use

    egrep '[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}' -o
    

    In action:

    $ echo 'Usage:       xp (UUID: 30503c82-bf04-4f75-ab8f-129b8b350487)' | egrep '[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}' -o
    30503c82-bf04-4f75-ab8f-129b8b35
    $ echo 'Usage:       xp (UUID: 30503c82-bf04-4f75-ab8f-129b8b350487)' | egrep 'UUID[^\)]+' -o
    UUID: 30503c82-bf04-4f75-ab8f-129b8b350487