我在一个文件中有一个包含以下内容的文件
192.168.168.23 pg.something
181.135.56.13 pg.nothing
15.123.96.12 l.everything
15.151.15.3 f.something
15.151.15.3 pg.something
64.196.12.34 pg.nothing
15.123.96.12 l.everything
181.168.56.13 pg.nothing
192.168.168.23 pg.something
192.168.168.23 l.everything
192.12.56.152 l.everything
181.135.56.13 pg.nothing
64.196.12.34 pg.nothing
64.196.12.34 pg.something
181.135.56.13 pg.nothing
64.196.12.34 l.everything
我试图找出每个 IP 对按 IP 排序的每个用户的命中数。
我试过了。
for i in `cat test_file |awk '{print $1}'|sort |uniq -c |sort -rn |awk '{print $2}'`; do grep $i test_file;done |uniq -c |awk '{print $2,$3,$1}'
得到了
64.196.12.34 pg.nothing 2
64.196.12.34 pg.something 1
64.196.12.34 l.everything 1
192.168.168.23 pg.something 2
192.168.168.23 l.everything 1
181.135.56.13 pg.nothing 3
15.151.15.3 f.something 1
15.151.15.3 pg.something 1
15.123.96.12 l.everything 2
192.12.56.152 l.everything 1
181.168.56.13 pg.nothing 1
这个输出没问题。但我想知道是否有办法将此输出修改为如下所示...
64.196.12.34 pg.nothing 2
pg.something 1
l.everything 1
192.168.168.23 pg.something 2
l.everything 1
181.135.56.13 pg.nothing 3
15.151.15.3 f.something 1
pg.something 1
15.123.96.12 l.everything 2
192.12.56.152 l.everything 1
181.168.56.13 pg.nothing 1
那是只删除重复的 IP...
提前致谢。
最佳答案
你可以修改你最后的 awk 命令:
awk '{if ($2!=a) {print $2"\t"$3"\t"$1} else {print "\t\t"$3"\t"$1}}{a=$2}'
给出:
64.196.12.34 pg.nothing 2
pg.something 1
l.everything 1
192.168.168.23 pg.something 2
l.everything 1
181.135.56.13 pg.nothing 3
15.151.15.3 f.something 1
pg.something 1
15.123.96.12 l.everything 2
192.12.56.152 l.everything 1
181.168.56.13 pg.nothing 1
关于linux - 使用列条件在 uniq 之后打印行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22063698/