首页 > 解决方案 > 如何在匹配表达式后面的同一行中使用 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)

有什么建议吗?

谢谢

标签: regexsed

解决方案


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+etcinto 单个命令替换为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)

ePerl 允许在带有标志的替换部分中使用 perl 代码。因此,您可以在一次替换中完成所有操作。lc用于小写转换,tr用于替换空格-等等。

对于过滤器+替换,请使用perl -ne 'print s/#+ (.+)/"- [$1](#" . lc($1) =~ tr| |-|r. ")"/re if /^#### /' ip.txt


推荐阅读