bash用于解析三个大括号之间的所有字符串

时间:2018-04-14 05:49:30

标签: string bash awk sed

我有以下文字:

{{{VSER}}} is doing well. He went to {{{MARKET}}}.
It is very useful {{{RESOURCE}}}

我想解析{{{}}}之间的字符串运行脚本echo $TEXT | somescript,然后结果应为:

VSER
MARKET
RESOURCE

尝试使用sed:

echo $TEXT | sed 's/.*{{{ //' | sed # /}}}.*$//'

但是,它会返回{{{VSER,甚至不返回VSER

2 个答案:

答案 0 :(得分:2)

实测!

echo $TEXT | awk -vRS="}}}" -vFS="{{{" '{print $2}'

感谢您的时间

答案 1 :(得分:0)

GNU grep有PCRE可用:

$ TEXT='{{{VSER     123}}} is doing well. He went to {{{MARKET}}}.
It is very useful {{{RESOURCE}}}'

$ echo $TEXT | grep -oP '\{\{\{\K.*?(?=}}})'
VSER 123
MARKET
RESOURCE

$ echo "$TEXT" | grep -oP '\{\{\{\K.*?(?=}}})'
VSER     123
MARKET
RESOURCE

$ # with perl
$ echo "$TEXT" | perl -lne 'print join "\n", /\{\{\{\K.*?(?=}}})/g'
VSER     123
MARKET
RESOURCE