如何将 grep 与日期格式和唯一值一起使用?

如何将 grep 与日期格式和唯一值一起使用?

我有大量的数据列表

我的数据看起来像这样

"[01/Dec/2011:20:53:04 +0900] ","COMZ","90.663.65.61","21.123.31.100","250","CONNECT","t.ierz.er:443","13127","836"
"[01/Dec/2011:22:20:01 +0900] ","COMZ","90.663.65.61","21.123.31.100","250","CONNECT","t.ierz.er:443","13127","836"
"[02/Dec/2011:24:33:04 +0900] ","COMZ","20.663.65.61","2.123.91.100","220","CONNECT","t.ierz.er:443","13127","836"

如何获取唯一值数据或 IP 地址等数据格式

01/DEC/2011 90.663.65.61 21.123.31.100

因为我已经得到了相同的值而无法得到独特的值

[01 / Dec / 2011: 20: 53: 04 0900] 90.663.65.61 21.123.31.100
[01 / Dec / 2011: 20: 53: 04 0900] 90.663.65.61 21.123.31.100

代码:

file.csv | awk -F\" '{print $2,$6,$8}' | sort | uniq -c | sort -n

答案1

您应该使用sed来完成您的请求。

这是一个适合您的情况的命令:

 cat file.csv | awk -F\" '{print $2,$6,$8}' | sed 's#\(:[[:digit:]]\{2\}\)\{3\} +0900##' | sort | uniq -c | sort -n

它将删除日期以仅保留以下格式:[01/DEC/2011] 90.663.65.61 21.123.31.100

答案2

尝试这个,

 awk -F '[:"[]' '{print $3" "$10" "$12}' file.csv | sort | uniq 

答案3

由于您的数据似乎采用 CSV 格式,您可能可以使用csvsqlfrom csvkit,请参阅https://csvkit.readthedocs.io/en/1.0.3/scripts/csvsql.html#

假设你的文件被命名为data.csv

csvsql -H --query 'SELECT a,c,d FROM data GROUP BY c,d' data.csv

印刷

a,c,d
[02/Dec/2011:24:33:04 +0900] ,20.663.65.61,2.123.91.100
[01/Dec/2011:22:20:01 +0900] ,90.663.65.61,21.123.31.100

也可以看看https://unix.stackexchange.com/a/495010/330217

答案4

我始终建议对 CSV 数据使用 CSV 解析器。这是红宝石:

ruby -rcsv -ne 'CSV.parse($_) do |row|
  puts [row[0][1..11].upcase, row[2], row[3]].join " "
end' | sort -u
01/DEC/2011 90.663.65.61 21.123.31.100
02/DEC/2011 20.663.65.61 2.123.91.100

相关内容