linux统计大文件行数的命令效率大对比,牛掰的awk 'END{print NR}' file

[Administrator@mc-win@/cygdrive/e/SoftWares/Project/log/click/2017-03-17@10:33:49]

517 $ time awk ‘{print NR}’ click.log_2017-03-17T04|tail -n1

116570

real    0m2.535s

user    0m1.499s

sys    0m1.093s

[Administrator@mc-win@/cygdrive/e/SoftWares/Project/log/click/2017-03-17@10:34:11]

518 $ time wc -l click.log_2017-03-17T04

116570 click.log_2017-03-17T04

real    0m19.201s

user    0m17.640s

sys    0m0.078s

[Administrator@mc-win@/cygdrive/e/SoftWares/Project/log/click/2017-03-17@10:34:38]

519 $ time awk ‘END{print NR}’ click.log_2017-03-17T04

116570

real    0m0.375s

user    0m0.343s

sys    0m0.031s

[Administrator@mc-win@/cygdrive/e/SoftWares/Project/log/click/2017-03-17@10:35:22]

520 $ time grep -n “” click.log_2017-03-17T04|awk -F: ‘{print ‘}|tail -n1

grep: bad regex ”: empty (sub)expression

real    0m0.084s

user    0m0.030s

sys    0m0.030s

[Administrator@mc-win@/cygdrive/e/SoftWares/Project/log/click/2017-03-17@10:36:15]

521 $ time grep -n “” click.log_2017-03-17T04|awk -F: ‘{print }’|tail -n1

grep: bad regex ”: empty (sub)expression

real    0m0.082s

user    0m0.030s

sys    0m0.015s

[Administrator@mc-win@/cygdrive/e/SoftWares/Project/log/click/2017-03-17@10:36:25]

522 $ time sed -n ‘$=’ click.log_2017-03-17T04

116570

real    0m0.885s

user    0m0.546s

sys    0m0.046s

总结 : time awk ‘END{print NR}’ click.log_2017-03-17T04
 执行最快,是 wc -l file 的几十倍的效率!!

还有  cat test1.sh |wc -l
这种就不要测试了,会把CPU用坏的吧!

time grep -n “” click.log_2017-03-17T04|awk -F: ‘{print }’|tail -n1

这个是人家博客写错了,正确的是:

time grep -n “” click.log_2017-03-17T04|awk -F: ‘{print $1}’|tail -n1

表示只是截取行号

上述grep 命令要求其支持””作为查找模式,windows上的busybox 1.6版的grep不支持该功能。linux支持:

[root@mc-click@/home/share@11:00:07]

2019 $ time awk ‘END{print NR}’ click_oom

250

real    0m0.013s

user    0m0.000s

sys    0m0.001s

[root@mc-click@/home/share@11:00:36]

2020 $ time grep -n “” click_oom |awk -F: ‘{print $1}’|tail -n1

250

real    0m0.004s

user    0m0.004s

sys    0m0.003s

    原文作者:五大RobertWu伍洋
    原文地址: https://www.jianshu.com/p/b18a1b40ea05
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞