gpt4 book ai didi

bash - 如何在 shell 中的指定行号中添加或删除字符?

转载 作者:行者123 更新时间:2023-11-29 09:46:31 26 4
gpt4 key购买 nike

我有一个文件,我必须遍历几次,每次都用 python 运行它。如何从 shell 中的特定行中删除或添加字符?

以ex.file为例,

$ cat ex.file
stuff
more stuff
more stuff
more stuff
#<- remove this character
<-add a pund sign here
more stuff
more stuff

我想要的输出是:

$ cat ex.file
stuff
more stuff
more stuff
more stuff
<- remove this character
#<-add a pund sign here
more stuff
more stuff

最佳答案

在第 6 行的开头添加#:

sed '6 s/^/#/' ex.file

从第 5 行删除前导#:

sed '5 s/^#//' ex.file

关于bash - 如何在 shell 中的指定行号中添加或删除字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38864538/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com