简体   繁体   English

如何在括号内grep模式?

[英]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 我想grep这种模式

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

I can use grep and sed to pick off it,using like this: 我可以使用grepsed来选择它,如下所示:

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

can i use only grep to accomplish this opration? 我可以仅使用grep来完成此操作吗?

You can use 您可以使用

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

which will include the "UUID:" prefix that your example code produces. 其中将包含示例代码生成的“ UUID:”前缀。

To get only the id, you can use 要仅获取ID,可以使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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