gpt4 book ai didi

linux - 在 shell 脚本中组合匹配的字符串和总和列

转载 作者:太空宇宙 更新时间:2023-11-04 05:46:30 25 4
gpt4 key购买 nike

如果 url 字符串中的第一个单词匹配,我想要总和计数。例如我想要的输出应该包含 url 中的总和和第一个单词

Count   Response    Url
3 400 data.internal.example.com
18 400 homeloans.internal.example.com
4 400 login.internal.example.com
465 400 login.internal.example.com
3 400 regions.internal.example.com
5 400 search.example.com
6 400 search.example.com
30 400 search.example.com
2 400 search.example.com
1 400 search.internal.example.com
1 422 login.example.com
1 422 login.example.com
139 422 newprojects.internal.example.com
1 422 notification.example.com
1 500 example.com
1 500 search.example.com

已使用 ruby​​ 代码和 shell 命令从日志文件中获取上述信息

result = `ruby -lane 'puts $F.values_at(9,8).join( \"\ \" )' #{@logfile} | grep -E '500\|502\|504\|400\|422\|409\|405'| grep -v "200" |grep -v "Nagar" | grep -v "Colony" |grep -v "Phase" | grep -v "Sector" | grep -v "Road" | grep -v "ignore_protected" |grep -v "LYF_LS_4002" | grep -v "utm_dynamicid" |sort |uniq -c`

下面应该是输出 -

Count   Response    Url
3 400 data
18 400 homeloans
469 400 login
3 400 regions
44 400 search
2 422 login
139 422 newprojects
1 422 notification
1 500 example.com
1 500 search.example.com

最佳答案

Perl 版本,带排序输出:

$ perl -lane 'next if $. == 1; # Skip header line
$F[2] =~ s/^[^.]+\K.*//; $recs{$F[1]}{$F[2]} += $F[0];
END { $, = "\t"; print "Count", "Response", "URL";
for $resp (sort keys %recs) {
for $url (sort keys %{$recs{$resp}}) {
print $recs{$resp}{$url}, $resp, $url
}}}' input.txt
Count Response URL
3 400 data
18 400 homeloans
469 400 login
3 400 regions
44 400 search
2 422 login
139 422 newprojects
1 422 notification
1 500 example
1 500 search
<小时/>

还有一个简短而甜蜜的版本,使用 GNU datamash (这假设列是制表符分隔的;如果不是,请将 -W 添加到 datamash 选项中)。

$ cut -d. -f1 input.txt | datamash -Hs groupby 2,3 sum 1 
GroupBy(Response) GroupBy(Url) sum(Count)
400 data 3
400 homeloans 18
400 login 469
400 regions 3
400 search 44
422 login 2
422 newprojects 139
422 notification 1
500 example 1
500 search 1

输出列的顺序不同,标题也不同,但如果需要的话,可以使用 awk 或其他方式轻松调整这些。

关于linux - 在 shell 脚本中组合匹配的字符串和总和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56882003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com