我想读一个有一些条件的文本文件。
111.196.10.1 - - [20/Jan/2020:07:00:50 +0100] "GET / HTTP/1.1" 200 123"
111.196.10.2 - - [20/Jan/2020:07:00:50 +0100] "GET /favicon.ico HTTP/1.1" 404 - "
111.196.10.3 - - [20/Jan/2020:07:00:50 +0100] "GET / HTTP/1.1" 200 206"
111.196.10.3 - - [20/Jan/2020:19:43:50 +0200] "GET /thttpd_powered_2.gif HTTP/1.1" 200 2114"
111.196.10.1 - - [20/Jan/2020:07:00:50 +0100] "GET / HTTP/1.1" 200 377"
111.196.10.3 - - [20/Jan/2020:07:00:50 +0100] "GET /thttpd_powered_2.gif HTTP/1.1" 200 2114"
111.196.10.2 - - [20/Jan/2020:07:00:50 +0100] "GET /sit-3-shine.7.gif HTTP/1.1" 404 - "
111.196.10.2 - - [20/Jan/2020:07:00:50 +0100] "GET / HTTP/1.1" 200 375"
111.196.10.1 - - [20/Jan/2020:07:00:50 +0100] "GET /sit3-shine.7.gif HTTP/1.1" 200 15811"
111.196.10.1 - - [20/Jan/2020:07:00:50 +0100] "GET /thttpd_powered_2.gif HTTP/1.1" 200 2114"
111.196.10.2 - - [20/Jan/2020:07:00:50 +0100] "GET / HTTP/1.1" 200 375"
111.196.10.3 - - [20/Jan/2020:07:00:50 +0100] "GET /sit3-shine.7.gif HTTP/1.1" 200 15811"
111.196.10.1 - - [20/Jan/2020:07:00:50 +0100] "GET /thttpd_powered_2.gif HTTP/1.1" 200 2114"
111.196.10.3 - - [20/Jan/2020:07:00:50 +0100] "GET / HTTP/1.1" 200 299"
111.196.10.2 - - [20/Jan/2020:07:00:50 +0100] "GET /sit3-shine.7.gif HTTP/1.1" 200 15811"
111.196.10.2 - - [20/Jan/2020:07:00:50 +0100] "GET /thttpd_powered_2.gif HTTP/1.1" 200 2114"
111.196.10.1 - - [20/Jan/2020:07:00:50 +0100] "GET / HTTP/1.1" 200 299"
111.196.10.2 - - [20/Jan/2020:07:00:50 +0100] "GET /thttpd_powered_2.gif HTTP/1.1" 200 2114"
111.196.10.3 - - [20/Jan/2020:07:00:50 +0100] "GET /sit3-shine.7.gif HTTP/1.1" 200 15811"
111.196.10.2 - - [20/Jan/2020:07:00:50 +0100] "GET /favicon.ico HTTP/1.1" 404 -"
111.196.10.1 - - [20/Jan/2020:07:00:50 +0100] "HEAD / HTTP/1.1" 304 299"
111.196.10.3 - - [20/Jan/2020:07:00:50 +0100] "GET / HTTP/1.1" 200 302"
111.196.10.1 - - [20/Jan/2020:07:00:50 +0100] "GET /thttpd_powered_2.gif HTTP/1.1" 200 2114"1:我想要一个脚本来计算文本文件中的IP地址,并给我这样的输出
111.196.10.1 8
111.196.10.2 8
111.196.10.3 7为此,我写了一个脚本。
cat file | awk '{print $1}' | sort | uniq -c | sort -nr | awk '{print $2" "$1}'它正常工作,但我希望它没有"awk“
2 :我的第二个要求是,只有当第二个行的最后一个数以2开头时,才算IP地址。
111.196.10.1 7
111.196.10.3 7
111.196.10.2 5为此,我写了一个剧本
grep '^[^"]*"[^"]*" 2' file | cut -d' ' -f1 | sort | uniq -c | sort -nr | awk '{print $2" "$1}'它也能正常工作,但我希望它没有"awk“
发布于 2020-11-07 12:15:30
剪切-f1 -d‘’文件排序-n \ uniq -c
如果第二个到最后一个数字以一个开始,那么只需要添加一个Grep就行了。
grep -E '20-9* 0-9+"$‘t\x\x -f1 -d’‘\x{e76f}\x{e76f}{##*$$}{##*$$}
在样本输入上测试。
发布于 2020-11-07 11:49:57
没有awk。
grep -oE '([0-9]+\.){3}[0-9]+' Input_file | sort | uniq -c用awk
请您试一试,在一个awk中完成它。
awk '
match($1,/([0-9]+\.){3}[0-9]+/){
arr[substr($0,RSTART,RLENGTH)]++
}
END{
for(key in arr){
print key,arr[key]
}
}
' Input_file或者,如果第一个字段仅为ip地址,则无需在数组索引中提到substr,我们可以直接使用$1,如下所示。
awk '
match($1,/([0-9]+\.){3}[0-9]+/){
arr[$1]++
}
END{
for(key in arr){
print key,arr[key]
}
}
' Input_file解释:添加了上面的详细说明。
awk ' ##Starting awk program from here.
match($1,/([0-9]+\.){3}[0-9]+/){ ##using match function to match IP address regex in current line.
arr[substr($0,RSTART,RLENGTH)]++ ##Create array arr which has index as sub string of matched regex from RSTART to RLENGTH.
}
END{ ##Starting END block of this program from here.
for(key in arr){ ##Traversing through arr from here.
print key,arr[key] ##printing key and array value here.
}
}
' Input_file ##Mentioning Input_file name here.发布于 2020-11-07 10:56:54
您可以使用read从输入读取两个字段,然后以不同的顺序回显它们。
所以替换
awk '{print $2" "$1}'使用
while read count ip; do
echo "$ip $count"
donehttps://stackoverflow.com/questions/64726740
复制相似问题