Hello,

On Fri, Jan 6, 2017 at 10:46 PM, Floris <jkflo...@dds.nl> wrote:
>>> So every Debian user has the perl command?
>>
>> Not only Debian users, the vast majority of linux / unix users have
>> perl installed (maybe now that android is here, this statement is not
>> true any more ...
>>
>> With awk:
>> awk -v vendor=0e11 'p == 1 && /^[^[:space:]]/ { p=0; } $0 ~ "^"vendor"
>> " {p=1;} p' /usr/share/misc/pci.ids
>>
>> With sed:
>> sed -ne '/^0e11/p' -e '/^0e11/,/^[^[:space:]]/ { /^[^[:space:]]/d ; p
>> }'  /usr/share/misc/pci.ids
>>
>> Wrapping to script which get an argument is easy
>>
>> Regards
>>
>
> Thanks for your answer, and the next question pops in.
>
> Is there a "rule" when I use perl, awk, sed, grep, cut etc. in a script?
> Often I find multiple solutions on the Internet to achieve the same output.
>
> A simple example:
>
> cut -d: -f2
> or
> awk -F: '{ print $2 }'
>
> Is the one better as the other (CPU/RAM usage), or is it just "taste"

I'm pretty sure that there are tools to help meassure ram / cpu used
by a shell script and then present result (max / min / media ...), but
I don't know any one (maybe memusg from a quick google search). I
would limit memory though ulimit -m to the memory that I would have to
run the script.

If a tool will not be executed thousands of times is irrelevant if it
is using cut / awk or another shell basic command. Why would you spend
more time on an script to optimize it if it won't run many times?
Even, if it will be executed by cron task, It is not important if it
takes one second, or ten seconds. Of course if it is a public script
is important, you don't know how many times it will be executed, the
less time take the script , less time are lost by future users ...

For a simple cpu benchmark, sorted by real and user % cpu:

$ time for i in {1..1000} ; do ./test_awk > /dev/null ; done

real    0m33,671s
user    0m28,384s
sys     0m1,228s
$ time for i in {1..1000} ; do ./test_bash > /dev/null ; done

real    0m29,404s
user    0m26,708s
sys     0m1,344s
$ time for i in {1..1000} ; do ./test_sed > /dev/null ; done

real    0m16,765s
user    0m12,452s
sys     0m0,820s
$ time for i in {1..1000} ; do ./test_perl > /dev/null ; done

real    0m15,932s
user    0m11,344s
sys     0m0,564s

# test_bash:

id="${1:-0e11}"
while IFS=\0 read line
do
    if [[ "$line" =~ ^$id[[:space:]].* ]]
    then
        echo "$line"
        p=1
        continue
    fi
    if [ "$p" = 1 ] && [[ "$line" =~ ^[[:space:]] ]]
    then
        echo "$line"
    fi
    if [[ "$line" =~ ^[^[:space:]] ]] && [ "$p" = 1 ]
    then
        exit 0
    fi

done  < /usr/share/misc/pci.ids

Regards,

Reply via email to