Linux/UNIX仅查看配置文件指令(配置文件的未注释行)
时间:2020-01-09 10:43:39 来源:igfitidea点击:
大多数Linux和类似UNIX的系统配置文件都是使用注释记录的,但是有些时候我只需要在配置文件中查看配置文本行即可。如何仅查看来自squid.conf或者httpd.conf文件的未注释的配置文件指令?如何在Linux或者类似Unix的系统上去除注释和空白行?要仅查看配置文件中未注释的文本行使用grep,sed,awk,perl或者UNIX/BSD/OS X/Linux操作系统提供的任何其他文本处理实用程序。
grep命令示例以删除命令
您可以使用gerp命令,如下所示:
$ grep -v "^#" /path/to/config/file $ grep -v "^#" /etc/apache2/apache2.conf
输出示例:
ServerRoot "/etc/apache2" LockFile /var/lock/apache2/accept.lock PidFile ${APACHE_PID_FILE} Timeout 300 KeepAlive On MaxKeepAliveRequests 100 KeepAliveTimeout 15 <IfModule mpm_prefork_module> StartServers 5 MinSpareServers 5 MaxSpareServers 10 MaxClients 150 MaxRequestsPerChild 0 </IfModule> <IfModule mpm_worker_module> StartServers 2 MinSpareThreads 25 MaxSpareThreads 75 ThreadLimit 64 ThreadsPerChild 25 MaxClients 150 MaxRequestsPerChild 0 </IfModule> <IfModule mpm_event_module> StartServers 2 MaxClients 150 MinSpareThreads 25 MaxSpareThreads 75 ThreadLimit 64 ThreadsPerChild 25 MaxRequestsPerChild 0 </IfModule> User ${APACHE_RUN_USER} Group ${APACHE_RUN_GROUP} AccessFileName .htaccess <Files ~ "^\.ht"> Order allow,deny Deny from all Satisfy all </Files> DefaultType text/plain HostnameLookups Off ErrorLog /var/log/apache2/error.log LogLevel warn Include /etc/apache2/mods-enabled/*.load Include /etc/apache2/mods-enabled/*.conf Include /etc/apache2/httpd.conf Include /etc/apache2/ports.conf LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%h %l %u %t \"%r\" %>s %O" common LogFormat "%{Referer}i -> %U" referer LogFormat "%{User-agent}i" agent CustomLog /var/log/apache2/other_vhosts_access.log vhost_combined Include /etc/apache2/conf.d/ Include /etc/apache2/sites-enabled/
要抑制空白行,请使用egrep命令,运行:
egrep -v "^#|^$" /etc/apache2/apache2.conf ## or pass it to the page such as more or less ## egrep -v "^#|^$" /etc/apache2/apache2.conf | less ## Bash function ###################################### ## or create function or alias and use it as follows ## ## viewconfig /etc/squid/squid.conf ## ####################################################### viewconfig(){ local f="" [ -f "" ] && command egrep -v "^#|^$" "$f" || echo "Error file not found." }
输出示例:
Unix/Linux Egrep注释掉空白行
了解grep/egrep命令行选项
-v选项可以反转匹配的含义,以选择不匹配的行。
此选项应在所有基于posix的系统上工作。
正则表达式^ $
匹配并删除所有空白行,而^^#
匹配并删除所有以#开头的注释。
sed命令示例
GNU/sed命令可以如下使用:
$ sed '/ *#/d; /^ *$/d' /path/to/file $ sed '/ *#/d; /^ *$/d' /etc/apache2/apache2.conf
GNU或者BSD sed也可以更新您的配置文件。
语法如下,以就地编辑文件,以指定扩展名(例如.bak)保存备份:
sed -i'.bak.2014.12.27' '/ *#/d; /^ *$/d' /etc/apache2/apache2.conf