[英]How to use sed within the same line behind match expression
我收到一條短信:
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)
格式:as/(\\(#[^ ]+) /\\1-/g; ta
這是一個循環,用於替換(#
請注意,您可以更換cat+grep+sed+etc
與成單個命令sed -nE '/^#### /{s/^#+ (.*)/- [\\1](#\\L\\1)/; :as/(\\(#[^ ]+) /\\1-/g; ta; p}' ip.txt
sed -nE '/^#### /{s/^#+ (.*)/- [\\1](#\\L\\1)/; :as/(\\(#[^ ]+) /\\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)
Perl 允許在帶有e
標志的替換部分中使用 perl 代碼。 因此,您可以在一次替換中完成所有操作。 lc
用於小寫轉換, tr
用於用-
替換空格,依此類推。
對於過濾器+替換,使用perl -ne 'print s/#+ (.+)/"- [$1](#" . lc($1) =~ tr| |-|r. ")"/re if /^#### /' ip.txt
這可能對你有用(GNU sed):
sed -E '/^#### /{s///;h;y/ /-/;H;g;s/(.*)\n(.*)/ - [\1](#\2)/}' file
對於以####
開頭的行,刪除散列。
將剩余部分復制到保留空間 (HS)。
用-
替換所有空格。
將修改后的行附加到 HS。
由 HS 替換當前線路。
第一行用方括號括起來,並在三個空格前加上破折號和一個空格,刪除換行符,在第二行前加上一個散列並用括號括起來。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.