方法一:使用sed
sed -e '/#/d' -e /^$/d'
[root@localhost ~]# cat /lnmp/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
[root@localhost ~]# sed -e '/#/d' -e '/^$/d' /lnmp/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
[root@localhost ~]#
可以通过 "-i "这个参数把结果写入配置文件。
方法二:通过vim里的删除命令
[root@localhost ~]# vim /lnmp/nginx/conf/nginx.conf
#删除空格
:g/^$/d
#删除#号
:g/^\s*#/d
通过wq!保存修改过内容。
各位亲爱的朋友,如有其他方法,欢迎补充