在日誌分析過程中,經常會遇到文件行計數的情況。它可以幫助我們分析業務數據。
那麼在Linux中如何使用linux命令行統計文件行數呢?
- 使用linux wc命令統計文件行數
<code>➜ wc -l test.txt/<code>
- 使用linux 管道、cat和wc命令統計文件行數
<code>➜ cat test.txt | wc -l/<code>
- 使用linux awk命令統計文件行數
<code>➜ awk 'END{print NR}' test.txt # OR ➜ awk '{print NR}' test.txt | tail -n1/<code>
- 使用sort命令、uniq命令和wc 命令統計文件非重複行的總數
<code>➜ cat test.txt | sort| uniq | wc -l/<code>
- 統計文件重複行的總數
<code># Sort and count the number of repetitions per row ➜ sort test.log | uniq -c # Number of lines with output repetition greater than 1 ➜ sort test.log | uniq -c | awk -F' ' '{if($1 > 1) { print $0 }}' # Count total number of duplicate lines ➜ sort test.log | uniq -c | awk -F' ' '{if($1 > 1) { print $0 }}' | wc -l /<code>
- 統計指定內容在文件中出現的次數
<code>➜ grep -c 'awk' test.log # OR ➜ grep 'awk' test.log | wc -l /<code>