简体   繁体   中英

grep within nested brackets

How do I grep strings in between nested brackets using bash? Is it possible without the use of loops? For example, if I have a string like:

[[TargetString1:SomethingIDontWantAfterColon[[TargetString2]]]]

I wish to grep only the two target strings inside the [[]] :

TargetString1
TargetString2

I tried the following command which cannot get TargetString2

grep -o -P '(?<=\[\[).*(?=\]\])'|cut -d ':' -f1

With GNU 's grep P option:

grep -oP "(?<=\[\[)[\w\s]+"

The regex will match a sequence of word characters ( \\w+ ) when followed by two brackets ( [[ ). This works for your sample string, but will not work for more complicated constructs like:

[[[[TargetString1]]TargetString2:SomethingIDontWantAfterColon[[TargetString3]]]]

where only TargetString1 and TargetString3 are matched.

To extract from nested [[]] brackets, you can use sed

#!/bin/bash

str="[[TargetString1:SomethingIDontWantAfterColon[[TargetString2]]]]"

echo $str | grep -o -P '(?<=\[\[).*(?=\]\])'|cut -d ':' -f1
echo $str | sed 's/.*\[\([^]]*\)\].*/\1/g' #which works only if string exsit between []

Output:

TargetString1
TargetString2

You can use grep regex grep -Eo '\\[\\[\\w+' | sed 's/\\[\\[//g' grep -Eo '\\[\\[\\w+' | sed 's/\\[\\[//g' for doing this

[root@localhost ~]# echo "[[TargetString1:SomethingIDontWantAfterColon[[TargetString2]]]]" | grep -Eo '\[\[\w+' | sed 's/\[\[//g'
TargetString1
TargetString2
[root@localhost ~]#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM