sed 对一系列行加上单行进行操作

ale*_*lex 1 sed

假设我有一个文件file.txt

hello 
world
hi
there
this
wow
Run Code Online (Sandbox Code Playgroud)

我知道 sed 可以在一行上运行,例如:

sed -n '2p' file.txt

或范围:

sed -n '2,4p' file.txt

但是如何在一个特定的行加上一个范围上进行操作?

sed -n '1line and 4,5' file.txt 或者用文字打印第一行和从第 4 行到第 5 行的范围??

Rav*_*h13 6

你能不能试试以下。使用 GNU 中显示的示例编写和测试sed。这将打印具有字符串 hello 的行,并将打印从第 2 行到第 4 行的行。

sed -n '/hello/p;2,4p' Input_file
Run Code Online (Sandbox Code Playgroud)

或者要给出单行号和行范围,请尝试以下操作:

sed -n '1p;2,4p' Input_file
Run Code Online (Sandbox Code Playgroud)

说明:使用-n选项首先停止所有行的打印。然后检查条件是否包含/hello行,然后按p选项打印该行。之后给出第二条语句,;以打印从第二行到第四行号的范围。