Linux 查找文本中重复的内容

Linux awk sort About 820 words

应用:需要查找日志中电话号码重复两次或两次以上的日志。

示例

文本:test.txt

{"phone":"15899999999","status":1, "time":1}
{"status":1, "time":2,"phone":"15811111111"}
{"phone":"15822222222", "status":1, "time":3}
{"status":1, "phone":"15899999999","time":4}
{"time":5,"phone":"15811111111", "status":1}

使用awksortuniq命令完成:

awk -F '"phone":"' '{print $2}' test.txt | awk -F '"' '{print $1}' | sort | uniq -d -c

输出:(第一列为重复次数)

      2 15811111111
      2 15899999999

解读

  1. 先以"phone":"分隔,取第2个字段,输出
15899999999","status":1, "time":1}
15811111111"}
15822222222", "status":1, "time":3}
15899999999","time":4}
15811111111", "status":1}
  1. 再以"分隔,取第1个字段,输出
15899999999
15811111111
15822222222
15899999999
15811111111
  1. 再进行sort排序,输出
15811111111
15811111111
15822222222
15899999999
15899999999
  1. 最后进行uniq -d -c统计重复,输出
      2 15811111111
      2 15899999999
Views: 4,652 · Posted: 2019-08-15

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh