Trying to get string between 2 delimiters

2019-09-02 10:22发布

I have a bit of an issue by trying to get a string between 2 delimiters using linux commands.

I am trying to retrieve TEST|LOL from "AA CC [[TEST|LOL]] EE FF" using sed.

I've used this command,but it gives me an error, invalid reference \1 on 1 s commands RHS

Command that i am using at the moment:

echo "AA CC [[TEST|LOL]] EE FF" | sed 's/.*[[\(.*\)]].*/\1/g'

So is there any possibility of fixing that command?Or maybe creating a bash script without the use of an IFS?

标签: linux bash sed
2条回答
Anthone
2楼-- · 2019-09-02 10:39

If your version of grep supports it, you can use the Perl regular expression mode:

echo "AA CC [[TEST|LOL]] EE FF" | grep -Po '\[\[\K[^\]]*'

The -o switch means that only the part of the input that matches the pattern is printed. The \K removes the first part of the pattern from the output.

查看更多
老娘就宠你
3楼-- · 2019-09-02 10:40

You must escape the [ and ] as well

$ echo "AA CC [[TEST|LOL]] EE FF" | sed 's/.*\[\[\(.*\)\]\].*/\1/g'
TEST|LOL
查看更多
登录 后发表回答