SED / TR等:如何注释掉包含&QUOT线;串"在一个文件? [英] SED/tr etc.. : How to comment out a line that contains "string" in a file?

查看:216
本文介绍了SED / TR等:如何注释掉包含&QUOT线;串"在一个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件所包含的行像这样的:

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

如何通过战略经济对话,或通过命令行一些其他的方式做到这一点?

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 '/ 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等:如何注释掉包含&QUOT线;串"在一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆