Reputation: 5
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
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
Reputation: 26667
You must escape the [
and ]
as well
$ echo "AA CC [[TEST|LOL]] EE FF" | sed 's/.*\[\[\(.*\)\]\].*/\1/g'
TEST|LOL
Upvotes: 1