在日志分析过程中,经常会遇到文件行计数的情况。它可以帮助我们分析业务数据。
那么在Linux中如何使用linux命令行统计文件行数呢?
- 使用linux wc命令统计文件行数
? wc -l test.txt
- 使用linux 管道、cat和wc命令统计文件行数
? cat test.txt | wc -l
- 使用linux awk命令统计文件行数
? awk 'END{print NR}' test.txt
# OR
? awk '{print NR}' test.txt | tail -n1
- 使用sort命令、uniq命令和wc 命令统计文件非重复行的总数
? cat test.txt | sort| uniq | wc -l
- 统计文件重复行的总数
# 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
- 统计指定内容在文件中出现的次数
? grep -c 'awk' test.log
# OR
? grep 'awk' test.log | wc -l