我正在尝试从“/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
答案 0 :(得分:2)
尝试
grep '^ServerName' /usr/local/apache/conf/httpd.conf
这是有效的,因为grep在模式中接受regular expressions。 ^
是任何一行的起始位置。
此外,请勿使用cat
将文件传递到grep
之类的内容,因为grep
有文件名参数。
答案 1 :(得分: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
使用您自己的文本替换print语句中的文本。
BEGIN部分将打印:#ServerName允许您设置发送回客户端的主机名
END部分将打印:#响应来自的服务器)它将使用ServerName和#ServerName host.some_domain.com
,其间的print语句将在两个ServerName条目之间打印语句。
答案 2 :(得分:0)
grep '^ServerName' your_file
或
perl -lne 'print if(/^ServerName[\s]+www/)' your_file