如果找不到空白,则在2个字符串之间查找文

时间:2014-10-03 16:51:12

标签: shell sed aix

我正在尝试在device_uuid:和

之间获取字符串

d

device_uuid:xxx,
ptr
device_uuid:2,

命令:

 sed -e 's/device_uuid:\(.*\),/\1/g' d

输出:

xxx
ptr
2

预期产出:

xxx
          == > blank as there is no pattern
2

1 个答案:

答案 0 :(得分:2)

这里需要一些更高级的sed命令:

sed 's/device_uuid:\([^,]*\),/\1/; tEnd; s/.*//; :End' <<DATA
device_uuid:xxx,
ptr
device_uuid:2,
DATA
xxx

2

如果先前的t命令进行了替换,s命令会跳转到标签,:命令会定义标签。

https://www.gnu.org/software/sed/manual/sed.html#Programming-Commands

使用换行符而不是分号可能更容易阅读

sed '
    s/device_uuid:\([^,]*\),/\1/
    tEnd
    s/.*//
    :End
'
相关问题