Команда cat vs grep vs awk получает содержимое файла, которое является более эффективным и менее трудоемким?

Допустим, у меня есть содержимое файла следующим образом:

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

Допустим, у меня есть содержимое файла следующим образом: [ ! d1]

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

Третий я попытался:

[ f6]

Выход:

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

Это означает, что коту нужна команда для печати всего содержимого файла. Поскольку для выполнения требуется меньше времени. ?

1
задан 24 December 2014 в 17:40

1 ответ

У меня одинаковые скрипты. В одном я использую 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
[d3 ] Время, затрачиваемое на первый скрипт, выглядит следующим образом (скрипт с операторами CAT):

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

Для второго скрипта, который имеет только инструкции AWK, выполняется следующее время:

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

Я думаю, что обработка awk файла намного быстрее по сравнению с вызовом другой внешней функции для чтения файлов. Я был бы рад за обсуждение результатов.

Я думаю, что AWK лучше работает в некоторых случаях.

0
ответ дан 24 May 2018 в 00:43

Другие вопросы по тегам:

Похожие вопросы: