【发布时间】:2021-10-05 13:27:27
【问题描述】:
我有变量
test="PlayLists: 00001.mpls 01:32:39 [12 chapters] 00005.mpls 00:19:37 [4 chapters] 00003.mpls 00:08:56 [1 chapters] 00004.mpls 00:00:39 [1 chapters] 00006.mpls 00:00:29 [2 chapters] 00007.mpls 00:00:25 [2 chapters] 00000.mpls 00:00:23 [1 chapters]"
我试过了:
chapters=$([[ $test =~ ((([0-9]+) chapters)+) ]] && echo "${BASH_REMATCH[1]}")
echo $chapters
但它只返回12 chatpters,我想得到
12 chapters
4 chapters
1 chapters
...
【问题讨论】:
-
如果您不需要纯 bash 解决方案,我只需使用
grep -Eo '[0-9]+ chapters'(try it here)。 -
@Aaron 这是示例输出,我编辑了帖子以添加...
-
@SnakeEyes 是的,我看到了,感谢您更新您的问题。 grep 解决方案或链接的 Q/A 是否令人满意?
-
是的,它成功了!谢谢
标签: bash