该文件包含以下行:
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
答案 0 :(得分: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