大多数的linux和类Unix系统的配置文件中都有许多的注释行,但是有时候我只想看其中的有效配置行。那我怎么才能只看到quID.conf或httpd.conf这样的配置文件中的非注释命令行呢?怎么去掉这些注释或者空行呢?我们可以使用 UNIX/BSD/OS X/linux 这些 *** 作系统自身提供的 grep,sed,awk,perl或者其他文本处理工具来查看配置文件中的有效配置命令行。
grep 命令示例——去掉注释可以按照如下示例使用grep命令:
1. `$ grep -v "^#" /path/to/config/file`2. `$ grep -v "^#" /etc/apache2/apache2.conf`
示例输出:
ServerRoot "/etc/apache2"Lockfile /var/lock/apache2/accept.lockPIDfile ${APACHE_PID_file}Timeout 300KeepAlive OnMaxKeepAliveRequests 100KeepAliveTimeout 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/plainHostnameLookups OffErrorLog /var/log/apache2/error.logLogLevel warnInclude /etc/apache2/mods-enabled/*.loadInclude /etc/apache2/mods-enabled/*.confInclude /etc/apache2/httpd.confInclude /etc/apache2/ports.confLogFormat "%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\"" combinedLogFormat "%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."}
示例输出:
图 01: Unix/linux Egrep 除去注释行和空行
-v 选项,选择出不匹配的命令行。该选项适用于所有基于posix的系统。正则表达式 ^$ 匹配出所有的非空行, ^# 匹配出所有的不以“#”开头的非注释行。
sed 命令示例sed去除注释行:
sed -i -c -e ‘/^#/d‘ config_file
sed去除空行:
sed -i -c -e ‘/^$/d‘ config_file
sed去空行和注释行:
sed -i -c -e ‘/^$/d;/^#/‘ config_file
可以按照如下示例使用 GNU 上的 sed 命令:
`$ sed ‘/ *#/d; /^ *$/d‘ /path/to/file``$ sed ‘/ *#/d; /^ *$/d‘ /etc/apache2/apache2.conf`
GNU 或 BSD 上的 sed 也可以修改配置文件。下面的命令的作用是原地编辑文件,并以特定(比如 .bak)备份文件:
`sed -i‘.bak.2015.12.27‘ ‘/ *#/d; /^ *$/d‘ /etc/apache2/apache2.conf`
更多信息见参考手册 - grep(1),sed(1)。
总结以上是内存溢出为你收集整理的如何只查看配置文件中未被注释的有效配置行全部内容,希望文章能够帮你解决如何只查看配置文件中未被注释的有效配置行所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)