本文介绍了SED/tr 等.:如何注释掉包含“字符串"的行?在文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的文件包含如下几行:
my file contains lines such as these:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2000 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2001 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2002 -j ACCEPT
我想注释掉(在前面加上#)
i want to comment out ( add # infront of ) the line that is
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2001 -j ACCEPT
我如何通过 SED 或通过命令行的其他方法执行此操作?
how can i do this via SED or some other method via command line ?
我应该寻找......例如.
should i seek for .. e.g..
2001
然后注释掉整行(在前面加上#)
and then comment out that whole line ( add # infront of )
或者我应该搜索整行,然后用包含 # 的新行替换它?
or should i search for the entire line and then replace it with a new one that contains # ?
最实用的方法是什么?(最快)
what would be the most practical method ? ( fastest )
推荐答案
通过 sed,
sed '/ 2001 /s/^/#/' file
添加内联编辑 -i
选项以保存所做的更改.
Add inline edit -i
option to save the changes made.
sed -i '/ 2001 /s/^/#/' file
示例:
$ sed '/ 2001 /s/^/#/' file
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2000 -j ACCEPT
#-A INPUT -m state --state NEW -m tcp -p tcp --dport 2001 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2002 -j ACCEPT
这篇关于SED/tr 等.:如何注释掉包含“字符串"的行?在文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!