示例 Markdown 文件:
cat index.md
# Abstract
- To achieve [Work Life Balance](./WorkLifeBalance/WorkLifeBalance.md), first understand what it means and what are the requirements.
- Develop right **[Attitude](./Attitude/index.md)** and [Haha](./Haha/Haha.md).
- Understand **[attention](./AttentionManagement/index.md)**, **[time](./TimeManagement/index.md)** and **[task](./TaskManagement/index.md)** management and start implementing those.
预期结果
[Work Life Balance](./WorkLifeBalance/WorkLifeBalance.md)
[Attitude](./Attitude/index.md)
[Haha](./Haha/Haha.md)
[attention](./AttentionManagement/index.md)
[time](./TimeManagement/index.md)
[task](./TaskManagement/index.md)
尝试解决
sed -r -n -e 's/.*(\[.*\]\(.*\)).*/\1/p' index.md
,但这只会匹配一行中的最后一个链接!
建议?
我更喜欢 sed,但如果 sed 无法实现,那么也欢迎使用其他工具。
答案1
我的解决方法:
sed -r -e 's/\[/\n[/g' index.md | sed -r -e 's/.*(\[.*\]\(.*\)).*/\n\1/g' | grep -oE '\[.*\]\(.*\)'
答案2
使用sed
$ sed -En '/\[/{s/[^[]*(\[[^)]*\))[^[]*/\1/g;/\[[^[]*\[/s/\[[^)]*\)/\n&/2g;p}' input_file
[Work Life Balance](./WorkLifeBalance/WorkLifeBalance.md)
[Attitude](./Attitude/index.md)
[Haha](./Haha/Haha.md)
[attention](./AttentionManagement/index.md)
[time](./TimeManagement/index.md)
[task](./TaskManagement/index.md)