Grep 从httpd.conf导出域名

Grep 从httpd.conf导出域名,grep,Grep,我正在尝试从“/usr/local/apache/conf/httpd.conf”导出域名,文件中的域名如下所示: ServerName www.site.com ServerName www2.site.org 我尝试使用: cat /usr/local/apache/conf/httpd.conf | grep ServerName 但输出将包含: # ServerName allows you to set a host name which is sent back to clien

我正在尝试从“/usr/local/apache/conf/httpd.conf”导出域名,文件中的域名如下所示:

ServerName www.site.com
ServerName www2.site.org
我尝试使用:

cat /usr/local/apache/conf/httpd.conf | grep ServerName
但输出将包含:

# ServerName allows you to set a host name which is sent back to clients for
ServerName www.site.com
# to the server the response is coming from) it will use ServerName and
#    ServerName host.some_domain.com
# ServerName allows you to set a host name which is sent back to clients for
ServerName www2.site.org
# to the server the response is coming from) it will use ServerName and
#    ServerName host.some_domain.com
试一试

grep'^ServerName'/usr/local/apache/conf/httpd.conf

这是有效的,因为grep在模式中接受<代码>^是任何行的起始位置

另外,不要使用
cat
将文件传递给
grep
之类的对象,因为
grep
有一个文件名参数。

试试awk

awk 'BEGIN{count=0;print "Text at top";}/ServerName/ {count++;if(count == 1){print "Text  
between two ServerName", count}}END{print "Text at the end"}' /usr/local/apache
/conf/httpd.conf
用自己的文本替换打印语句中的文本

“开始”部分将打印:#ServerName允许您设置主机名,该主机名将发送回客户端以供下载

结束部分将打印:#到响应来自的服务器)它将使用ServerName和#ServerName host.some_domain.com

中间的print语句将在两个ServerName条目之间打印该语句

grep '^ServerName' your_file


这正是问题的等价物。这不是真正的答案。与其解析
httpd.conf
,不如解析
/usr/local/sbin/httpd-t-D DUMP_VHOSTS
的输出,因为这样还可以找到其他文件中包含的虚拟主机。感谢帮助。现在,输出将类似于:“ServerName www.site.com ServerName wwww2.site.org”我能不能,以某种方式,让它成为:“www.site.com www2.site.org”@user2676847这是一个完全不同的问题,需要另一个线程。但我的解决方案是:
grep'^ServerName'/usr/local/apache/conf/httpd.conf | cut-f2--d'
perl -lne 'print if(/^ServerName[\s]+www/)' your_file