简体   繁体   中英

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?

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]{8}' -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]{8}' -o
30503c82-bf04-4f75-ab8f-129b8b35
$ echo 'Usage:       xp (UUID: 30503c82-bf04-4f75-ab8f-129b8b350487)' | egrep 'UUID[^\)]+' -o
UUID: 30503c82-bf04-4f75-ab8f-129b8b350487

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM