AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 564944
Accepted
snoop
snoop
Asked: 2014-12-25 02:16:12 +0800 CST2014-12-25 02:16:12 +0800 CST 2014-12-25 02:16:12 +0800 CST

cat vs grep vs awk 命令获取文件内容哪个效率更高且耗时更少?

  • 772

可以说我有这样的文件内容:

this is a simple file for testing purpose
with few lines in it.
to check the cat and grep command to verfy which is best and less excution time consuming

首先我试过:

time cat temp.txt

输出:

this is a simple file for testing purpose
with few lines in it.
to check the cat and grep command to verfy which is best and less excution time consuming

real    0m0.001s
user    0m0.000s
sys     0m0.001s

其次我试过:

time grep "$"  temp.txt

输出:

this is a simple file for testing purpose
with few lines in it.
to check the cat and grep command to verfy which is best and less excution time consuming

real    0m0.002s
user    0m0.000s
sys     0m0.002s

第三我试过:

time awk  "/$/"  temp.txt

输出:

this is a simple file for testing purpose
with few lines in it.
to check the cat and grep command to verfy which is best and less excution time consuming

real    0m0.004s
user    0m0.001s
sys     0m0.004s

和:

time awk 1 temp.txt

输出:

this is a simple file for testing purpose
with few lines in it.
to check the cat and grep command to verfy which is best and less excution time consuming

real    0m0.004s
user    0m0.000s
sys     0m0.003s

使用 sed:

time sed "" temp.txt

输出:

this is a simple file for testing purpose
with few lines in it.
to check the cat and grep command to verfy which is best and less excution time consuming

real    0m0.002s
user    0m0.000s
sys     0m0.002s

这意味着 cat 是打印所有文件内容的更好命令。因为它执行所需的时间更少。?

grep
  • 2 2 个回答
  • 6141 Views

2 个回答

  • Voted
  1. Best Answer
    Sparhawk
    2014-12-25T03:23:25+08:002014-12-25T03:23:25+08:00

    答案是“是”。最初,这更像是一个断言,因为 cat 只是在读取文件,而其他两个正在扫描文件以查找表达式。您的time脚本是正确的想法,但在这些极短的持续时间内,任何小的差异都会产生错误的结果。最好使用更大的文件,或重复多次。

    $ time for i in {1..1000}; do cat temp.txt; done
    ...
    real    0m0.762s
    user    0m0.060s
    sys     0m0.147s
    
    $ time for i in {1..1000}; do grep "$" temp.txt; done
    ...
    real    0m3.128s
    user    0m0.667s
    sys     0m0.263s
    
    $ time for i in {1..1000}; do awk "/$/" temp.txt; done
    ...
    real    0m3.332s
    user    0m0.720s
    sys     0m0.337s
    

    此外(未显示),我多次运行上述命令以确认每个命令大约同时运行,因此是可复制的。

    更多基准

    根据评论,这里有一些我测试过的命令。在我的系统上,虽然接近了,但效率grep "^"并awk "1"没有明显提高。sed ""cat

    $ time for i in {1..1000}; do grep "^" temp.txt; done
    ...
    real    0m2.992s
    user    0m0.527s
    sys     0m0.303s
    
    $ time for i in {1..1000}; do awk "1" temp.txt; done
    ...
    real    0m3.185s
    user    0m0.570s
    sys     0m0.317s
    
    $ time for i in {1..1000}; do sed "" temp.txt; done
    ...
    real    0m0.983s
    user    0m0.077s
    sys     0m0.193s
    
    • 3
  2. Himanshu Chauhan
    2016-07-22T23:34:09+08:002016-07-22T23:34:09+08:00

    我有相同的脚本。在一个中,我使用的是 cat,而在另一个中,它全是 AWK。

    这是第一个:

    #!/bin/bash
    
    
            lines=$(cat /etc/passwd | wc -l)
    
            for ((i=1 ; i <=$lines ; i++ ))
            do
            user=$(cat /etc/passwd | awk -F : -vi=$i 'NR==i {print $1}')
            uid=$(cat /etc/passwd | awk -F : -vi=$i 'NR==i {print $3}')
            gid=$(cat /etc/passwd | awk -F : -vi=$i 'NR==i {print $4}')
            shell=$(cat /etc/passwd | awk -F : -vi=$i 'NR==i {print $7}')
            echo -e "User is : $user \t Uid is : $uid \t Gid is : $gid \t Shell is : $shell"
            done
    

    这是第二个:

    #!/bin/bash
    
    
            lines=$(awk  'END {print NR}' /etc/passwd)
    
            for ((i=1 ; i <=$lines ; i++ ))
            do
            user=$(awk  -F : -vi=$i 'NR==i {print $1}' /etc/passwd)
            uid=$(awk  -F : -vi=$i 'NR==i {print $3}'  /etc/passwd)
            gid=$(awk  -F : -vi=$i 'NR==i {print $4}'  /etc/passwd)
            shell=$(awk  -F : -vi=$i 'NR==i {print $7}' /etc/passwd)
            echo -e "User is : $user \t Uid is : $uid \t Gid is : $gid \t Shell is : $shell"
            done
    

    第一个脚本所用时间如下(带有 CAT 语句的脚本):

    real    0m0.215s
    user    0m0.023s
    sys     0m0.238s
    

    对于只有 AWK 语句的第二个脚本,所用时间如下:

    real    0m0.132s
    user    0m0.013s
    sys     0m0.123s
    

    我认为与调用其他外部函数读取文件相比,awk 处理文件要快得多。我很乐意就结果进行讨论。

    我认为 AWK 在某些情况下表现更好。

    • 0

相关问题

  • 如何让 grep 只显示匹配的正则表达式?

  • 如何 grep 字符串变量的内容?

  • 拖尾两个日志文件

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve