1

该文件包含如下行:

1363650803.076     48 192.168.20.58 TCP_MISS/417 1963 POST http://collector-4.newrelic.com/agent_listener/invoke_raw_method? - NONE/- text/html
1363650863.426      0 192.168.20.58 TCP_MISS/417 1963 POST http://collector-4.newrelic.com/agent_listener/invoke_raw_method? - NONE/- text/html
1363650898.698  63995 192.168.20.61 TCP_MISS/000 0 GET http://65.55.122.235/2.0/blugro5relay.groove.microsoft.com/p9xyqk93z98qti59r7taftbcxe3s6rt52n4drjs,ConnType=LongLived,ContentLength=2147479552 - DIRECT/65.55.122.235 -
1363650898.698  63995 192.168.20.61 TCP_MISS/000 0 POST http://65.55.122.235/2.0/blugro5relay.groove.microsoft.com/p9xyqk93z98qti59r7taftbcxe3s6rt52n4drjs,ConnType=LongLived - DIRECT/65.55.122.235 -
1363650899.199  63991 192.168.20.61 TCP_MISS/000 0 GET http://65.55.122.235/2.0/blugro5relay.groove.microsoft.com/xatanmtq99upj97qtps89zqnx99wqsyeuc5yefa,ConnType=KeepAlive - DIRECT/65.55.122.235 -
1363650899.199  63991 192.168.20.61 TCP_MISS/000 0 POST http://65.55.122.235/2.0/blugro5relay.groove.microsoft.com/xatanmtq99upj97qtps89zqnx99wqsyeuc5yefa,ConnType=KeepAlive - DIRECT/65.55.122.235 -

我想从每一行中获取主机名/IP 地址。

这个 sed 命令有什么问题?

sed -rn "s@//([^/]+)/@\1@" access.log
4

1 回答 1

3

首先,-n避免打印每一行。去掉它。

其次,匹配整行并将其全部替换为分组的内容。

第三,在前面添加http:一个//更准确的匹配。

就像:

sed -r "s@^.*http://([^/]+).*@\1@" infile

这会产生:

collector-4.newrelic.com
collector-4.newrelic.com
65.55.122.235
65.55.122.235
65.55.122.235
65.55.122.235
于 2013-03-21T18:13:13.443 回答