0

我正在尝试从以下位置导出域名:“/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
4

3 回答 3

2

尝试

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

这是有效的,因为 grep 接受模式中的正则表达式^是任何一行的起始位置。

另外,不要用于cat将文件传递给类似的东西grep,因为grep有一个文件名参数。

于 2013-08-22T04:55:15.220 回答
0

试试 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

用您自己的文本替换打印语句中的文本。

BEGIN 部​​分将打印:# ServerName 允许您设置一个主机名,该主机名将发送回客户端

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

并且中间的打印语句将打印两个 ServerName 条目之间的语句。

于 2013-08-22T04:56:21.727 回答
0
grep '^ServerName' your_file

或者

perl -lne 'print if(/^ServerName[\s]+www/)' your_file
于 2013-08-22T07:06:03.447 回答