
Apache
ログファイルがありますaccess.log
。ファイル内の行数をどのように計算できますか?たとえば、結果cut -f 7 -d ' ' | cut -d '?' -f 1 | tr '[:upper:]' '[:lower:]'
は次のようになります。
a.php
b.php
a.php
c.php
d.php
b.php
a.php
私が望む結果は次のとおりです。
3 a.php
2 b.php
1 d.php # order doesn't matter
1 c.php
答え1
答え2
[your command] | sort | uniq -c | sort -nr
sort -nr
許可された回答はほぼ完了しました。最も頻繁に発生する行を最初に配置するために、最後に追加の項目を追加できます。
ユニークオプション:
-c, --count
prefix lines by the number of occurrences
タイプオプション:
-n, --numeric-sort
compare according to string numerical value
-r, --reverse
reverse the result of comparisons
特別な場合に並べ替える行が数字の場合は、代わりにを使用する必要がsort -gr
ありますsort -nr
。コメント
答え3
連想配列を使用できますアッ次に -オプション -タイプ:
$ awk ' { tot[$0]++ } END { for (i in tot) print tot[i],i } ' access.log | sort
出力:
1 c.php
1 d.php
2 b.php
3 a.php
答え4
サンプルは1つだけですd.php
。そうすれば、こんなに良い結果が出るでしょう。
wolf@linux:~$ cat file | sort | uniq -c
3 a.php
2 b.php
1 c.php
1 d.php
wolf@linux:~$
4つあればどうなりますかd.php
?
wolf@linux:~$ cat file | sort | uniq -c
3 a.php
2 b.php
1 c.php
4 d.php
wolf@linux:~$
発生順に出力をソートするには、sort
stdoutを再送信する必要があります。
wolf@linux:~$ cat file | sort | uniq -c | sort
1 c.php
2 b.php
3 a.php
4 d.php
wolf@linux:~$
-r
リバース
wolf@linux:~$ cat file | sort | uniq -c | sort -r
4 d.php
3 a.php
2 b.php
1 c.php
wolf@linux:~$
この例が役に立つことを願っています。