linux 删除文件的注释行

常用的方法来删除文件中的注释行

方法一:

采用grep命令的-v选项,输出除之外的所有行,容后重定向输出到配置文件。

$ cp xxx.conf xxx.conf.bak
#删除注释行到配置文件中
$ grep -v '^#' xxx.conf > conf.con
#删除文件中的空行
$ grep -v '^$' xxx.conf

#删除注释行和空行
$ grep -v '^#' xxx.conf | grep -v '^$' > xxx.conf

方法二:

使用sed命令:

$ sed '/^#/d' <file>
$ sed '/#/d'  <file>

# 删除空行
$ sed '/^$/d' <file>
# 删除空行并写入到文件
$ sed -i '/^$/d' <file>

参考:https://blog.csdn.net/robertkun/article/details/9

猜你喜欢

转载自blog.csdn.net/hl449006540/article/details/79999399