Tudorut Robert
Tudorut Robert

Reputation: 5

Trying to get string between 2 delimiters

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?

Upvotes: 0

Views: 345

Answers (2)

Tom Fenech
Tom Fenech

Reputation: 74595

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.

Upvotes: 0

nu11p01n73R
nu11p01n73R

Reputation: 26667

You must escape the [ and ] as well

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

Upvotes: 1

Related Questions