格式化grep命令的输出

时间:2014-02-04 13:32:18

标签: apache formatting grep output

我有一个运行某些应用程序的网络服务器。我从服务器执行grep以查看已连接到服务器的所有IP。我实现了它,但HTML看起来像这样:

172.17.100.37 172.17.100.45 172.17.222.158

我希望看到这样:

172.17.100.37 
172.17.100.45 
172.17.222.158

要获得这些值,我会这样做:

cmd = "grep -o '^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' /var/log/apache2/access.log | sort | uniq "

关于我能做什么的任何想法?

编辑:access.log显示如下:

172.17.222.158 - - [30/Jan/2014:09:33:11 +0000] "GET /cgi-bin/right.cgi HTTP/1.1" 204  219 "http://172.17.223.72/index2.html?fname=172.17.223.75" "Mozilla/5.$
172.17.222.158 - - [30/Jan/2014:09:33:11 +0000] "GET /cgi-bin/stop.cgi HTTP/1.1" 204 218 "http://172.17.223.72/index2.html?fname=172.17.223.75" "Mozilla/5.0$
172.17.222.158 - - [30/Jan/2014:09:33:12 +0000] "GET /cgi-bin/right.cgi HTTP/1.1" 204 218 "http://172.17.223.72/index2.html?fname=172.17.223.75" "Mozilla/5.$
172.17.222.158 - - [30/Jan/2014:09:33:12 +0000] "GET /cgi-bin/stop.cgi HTTP/1.1" 204 

依旧......

2 个答案:

答案 0 :(得分:1)

据我所知,您将grep-command的输出粘贴到HTML文件中。 要获得换行,您可以使用预标签:
< pre> (您的输出到此处) < / pre>
这将在HTML中显示您的换行符。

答案 1 :(得分:0)

使用tr

echo "172.17.100.37 172.17.100.45 172.17.222.158" | tr ' ' '\n'
172.17.100.37
172.17.100.45
172.17.222.158

使用awk

cat file
172.17.222.158 - - [30/Jan/2014:09:33:11 +0000] "GET /cgi-bin/right.cgi HTTP/1.1" 204  219 "http://172.17.223.72/index2.html?fname=172.17.223.75" "Mozilla/5.$
172.17.222.158 - - [30/Jan/2014:09:33:11 +0000] "GET /cgi-bin/stop.cgi HTTP/1.1" 204 218 "http://172.17.223.72/index2.html?fname=172.17.223.75" "Mozilla/5.0$
172.17.222.158 - - [30/Jan/2014:09:33:12 +0000] "GET /cgi-bin/right.cgi HTTP/1.1" 204 218 "http://172.17.223.72/index2.html?fname=172.17.223.75" "Mozilla/5.$
172.17.222.158 - - [30/Jan/2014:09:33:12 +0000] "GET /cgi-bin/stop.cgi HTTP/1.1" 204 

awk -F/ '{print $8}' file
172.17.223.72
172.17.223.72
172.17.223.72