knk*_*knk 3 sed shell-script text-processing
我有一些这样的文字:
Sentence #1 (n tokens):
Blah Blah Blah
[...
...
...]
( #start first set here
... (other possible parens and text here)
) #end first set here
(...)
(...)
Sentence #2 (n tokens):
Run Code Online (Sandbox Code Playgroud)
我想提取第二组括号(包括中间的所有内容),即,
(
... (other possible parens here)
)
Run Code Online (Sandbox Code Playgroud)
有没有一种 bash 方法来做到这一点。我尝试了简单的
's/(\(.*\))/\1/'
Run Code Online (Sandbox Code Playgroud)
这将做到。可能有更好的方法,但这是想到的第一种方法:
echo 'Sentence #1 (n tokens):
Blah Blah Blah
[...
...
...]
(
... (other possible parens here)
)
(...)
(...)
Sentence #2 (n tokens):
' | perl -0777 -nE '
$wanted = 2;
$level = 0;
$text = "";
for $char (split //) {
$level++ if $char eq "(";
$text .= $char if $level > 0;
if ($char eq ")") {
if (--$level == 0) {
if (++$n == $wanted) {
say $text;
exit;
}
$text="";
}
}
}
'
Run Code Online (Sandbox Code Playgroud)
产出
(
... (other possible parens here)
)
Run Code Online (Sandbox Code Playgroud)