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

标签 bash shell unix sed

我有一个文件,我必须遍历几次,每次都用 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/

相关文章:

python - 使用参数激活(源)python virtualenv 的 Bash 脚本

bash - 带有子 shell 的 'cd' 命令的别名未按预期工作

java - 什么会导致bash无法执行在我自己的机器上编写和编译的.jar?

multithreading - 使用 bash 进行多线程

linux - 如何停止在 Shell 脚本中换行

linux - 给定与之关联的 ip 地址,如何获取 NIC 卡的逻辑名称?

bash - 如何在 shell 脚本中抑制命令的输出?

c - C-如何解析反斜杠字符

linux - 使用 sudo su 访问将文件从 Linux 盒子复制到远程主机的 shell 脚本

c - C中的中止函数