regex - 如何在匹配表达式后面的同一行中使用 sed
问题描述
我收到一条短信:
cat README.md | grep "####.*"
#### Creating a helm chart with 2 child helm chart helm v3
#### Check you helmchart repositories
#### List content of your main-helmchart
#### Helm v3 dependency update
#### Check the content of charts/ folder in main-helmchart/
#### Check what is going to be installed
我试图让它看起来像这样:
- [Creating a helm chart with 2 child helm chart helm v3](#creating-a-helm-chart-with-2-child-helm-chart-helm-v3)
- [Check you helmchart repositories](#check-you-helmchart-repositories)
- [List content of your main-helmchart](#list-content-of-your-main-helmchart)
- [Helm v3 dependency update](#helm-v3-dependency-update)
- [Check the content of charts/ folder in main-helmchart/](#check-the-content-of-charts/-folder-in-main-helmchart/)
- [Check what is going to be installed](#check-what-is-going-to-be-installed)
有什么方法可以为此使用sed吗?
我正在尝试匹配“#”符号后面的所有内容 并替换破折号“-”的空格
cat README.md | sed -E -n 's/^([#]{4}\s)(.*)$/- [\2](#\L\2)/p' | sed -E '/#.*/ s/ /-/g'
--[Creating-a-helm-chart-with-2-child-helm-chart-helm-v3](#creating-a-helm-chart-with-2-child-helm-chart-helm-v3)
--[Check-you-helmchart-repositories](#check-you-helmchart-repositories)
--[List-content-of-your-main-helmchart](#list-content-of-your-main-helmchart)
--[Helm-v3-dependency-update](#helm-v3-dependency-update)
--[Check-the-content-of-charts/-folder-in-main-helmchart/](#check-the-content-of-charts/-folder-in-main-helmchart/)
--[Check-what-is-going-to-be-installed](#check-what-is-going-to-be-installed)
有什么建议吗?
谢谢
解决方案
和GNU sed
$ sed -E 's/^#+ (.*)/- [\1](#\L\1)/; :a s/(\(#[^ ]+) /\1-/g; ta' ip.txt
- [Creating a helm chart with 2 child helm chart helm v3](#creating-a-helm-chart-with-2-child-helm-chart-helm-v3)
- [Check you helmchart repositories](#check-you-helmchart-repositories)
- [List content of your main-helmchart](#list-content-of-your-main-helmchart)
- [Helm v3 dependency update](#helm-v3-dependency-update)
- [Check the content of charts/ folder in main-helmchart/](#check-the-content-of-charts/-folder-in-main-helmchart/)
- [Check what is going to be installed](#check-what-is-going-to-be-installed)
s/^#+ (.*)/- [\1](#\L\1)/
这匹配#### Heading
并转换为- [Heading](#heading)
格式:a s/(\(#[^ ]+) /\1-/g; ta
这是一个循环替换之后找到的所有空格(#
请注意,您可以将您的cat+grep+sed+etc
into 单个命令替换为sed -nE '/^#### /{s/^#+ (.*)/- [\1](#\L\1)/; :a s/(\(#[^ ]+) /\1-/g; ta; p}' ip.txt
和perl
$ perl -pe 's/#+ (.+)/"- [$1](#" . lc($1) =~ tr| |-|r. ")"/e' ip.txt
- [Creating a helm chart with 2 child helm chart helm v3](#creating-a-helm-chart-with-2-child-helm-chart-helm-v3)
- [Check you helmchart repositories](#check-you-helmchart-repositories)
- [List content of your main-helmchart](#list-content-of-your-main-helmchart)
- [Helm v3 dependency update](#helm-v3-dependency-update)
- [Check the content of charts/ folder in main-helmchart/](#check-the-content-of-charts/-folder-in-main-helmchart/)
- [Check what is going to be installed](#check-what-is-going-to-be-installed)
e
Perl 允许在带有标志的替换部分中使用 perl 代码。因此,您可以在一次替换中完成所有操作。lc
用于小写转换,tr
用于替换空格-
等等。
对于过滤器+替换,请使用perl -ne 'print s/#+ (.+)/"- [$1](#" . lc($1) =~ tr| |-|r. ")"/re if /^#### /' ip.txt
推荐阅读
- ruby-on-rails - 用于删除外键的 RESTFUL 端点
- chef-infra - 厨师刀命令显示所有属性
- c++ - netbean 和 cygwin 的 c++ 编译错误?
- python - 如何打印包含字节字符串的 Python 字典?
- swift - RxSwift 等效 BehaviorSubject 的 .modify() 方法
- reactjs - ReactJS:将 Base64 数据保存到服务器上的 PNG 文件(用于在社交媒体网站上共享)
- docker - 从 Ubuntu 16.04 到 CentOS 6.9 的 Docker 映像
- php - 输出变量未按预期工作
- python - 在 Python GTK3 中监听 Control + 鼠标滚轮滚动事件
- amazon-web-services - DynamoDB 仪表板无法保存/更新项目:ConditionalCheckFailedException