简体   繁体   English

使用sed替换文本文件中2行之间的文本

[英]Replace text between 2 particular lines in a text file using sed

Similar questions have been asked but they are for Powershell. 有人问过类似的问题,但它们是针对Powershell的。

I have a Markdown file like: 我有一个Markdown文件,例如:

.
.
.
## See also

- [a](./A.md)
- [A Child](./AChild.md)
.
.
.
- [b](./B.md)
.
.
.
## Introduction
.
.
.

I wish to replace all occurrences of .md) with .html) between ## See also and ## Introduction : 我希望在## See also## Introduction之间将所有出现的.md)替换为.html)

.
.
.
## See also

- [a](./A.html)
- [A Child](./AChild.html)
.
.
.
- [b](./B.html)
.
.
.
## Introduction
.
.
.

I tried like this in Bash 我在Bash中尝试过这样

orig="\.md)"; new="\.html)"; sed "s~$orig~$new~" t.md -i

But, this replaces everywhere in the file. 但是,这将替换文件中的所有位置。 But I wish that the replacement happens only between ## See also and ## Introduction 但我希望替换只发生在## See also## Introduction

Could you please suggest changes? 你能建议改变吗? I am using awk and sed as I am little familiar with those. 我正在使用awk和sed,因为我对此不太熟悉。 I also know a little Python, is it recommended to do such scripting in Python (if it is too complicated for sed or awk)? 我也知道一些Python,是否建议在Python中进行此类脚本编写(如果对于sed或awk而言过于复杂)?

$ sed '/## See also/,/## Introduction/s/\.md/.html/g' file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM