linux - 使用列条件在 uniq 之后打印行

标签 linux bash sorting awk uniq

我在一个文件中有一个包含以下内容的文件

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/

相关文章:

c# Hashtable 按键排序

java - Raspberry Pi 和 javaFX 命令错误

c - fopen中的r和rb有什么区别

linux - 404 路由问题 - AWS Linux/Wordpress 站点

bash - 如何在 'yum -y update' 运行时每 2 秒回显一个句点?

java - 扫描仪输入数组排序

c - 我如何在 C 中找到可以免费使用的端口?

bash - 在 csv 文件中查找重复项的脚本

linux - 删除超过 30 天的文件,但在 1 个目录中保留 6 个月

python - 对键也是字典的字典键进行排序