LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   sed or grep (http://www.linuxquestions.org/questions/linux-general-1/sed-or-grep-4175426902/)

the_gripmaster 09-12-2012 11:56 AM

sed or grep
 
Following two commands do the same thing:

Code:

sed -ne '/^dog$/p' animallist.txt
Code:

grep '^dog$' animallist.txt
How do I find out which one is more efficient: uses less memory and less processor time? Using time will give the processor time but are there any other ways to find the memory and processor usage?

cortman 09-12-2012 04:19 PM

If you're just trying to find text, not replace it, grep seems the more natural tool to me.
I guess I can't confirm that it's any faster/slower, though.

Valery Reznic 09-13-2012 12:49 PM

Quote:

Originally Posted by the_gripmaster (Post 4778643)
Following two commands do the same thing:

Code:

sed -ne '/^dog$/p' animallist.txt
Code:

grep '^dog$' animallist.txt
How do I find out which one is more efficient: uses less memory and less processor time? Using time will give the processor time but are there any other ways to find the memory and processor usage?

Not exactly the same.
if there is no dog in animallist then grep will return exit status 1, while sed - 0.

If command used in the script exit status may be important.
About CPU and memory - no idea. Unless file is very big I don't think
you'll fell any practical difference

the_gripmaster 09-13-2012 12:53 PM

Quote:

Originally Posted by Valery Reznic (Post 4779555)
Not exactly the same.
if there is no dog in animallist then grep will return exit status 1, while sed - 0.

If command used in the script exit status may be important.
About CPU and memory - no idea. Unless file is very big I don't think
you'll fell any practical difference

That's correct, $? will be different. However, the output to stdout will be the same. I guess strace might give an idea of which one is more efficient?

Valery Reznic 09-13-2012 01:38 PM

Quote:

Originally Posted by the_gripmaster (Post 4779558)
That's correct, $? will be different. However, the output to stdout will be the same. I guess strace might give an idea of which one is more efficient?

Why efficiency is such a big issue? Do you expect to process huge files?

sycamorex 09-13-2012 01:47 PM

Quote:

Originally Posted by the_gripmaster (Post 4778643)
]How do I find out which one is more efficient: uses less memory and less processor time? Using time will give the processor time but are there any other ways to find the memory and processor usage?

As others said, if the input files are not large, other aspects (eg. exit status) may play a bigger role. If you're working with huge files, you can compare the following:

Code:

$ time your_sed_command

Code:

$ time your_grep_command
Both commands will output some stats, eg:

Code:

# time updatedb
updatedb  0.81s user 0.96s system 95% cpu 1.849 total


chrism01 09-14-2012 01:43 AM

There's also the GNU version of time (\time) for more detail eg
Code:

cd /
time ls
bin  cgroup  etc  lib    lost+found  misc  net  proc  sbin    srv  tmp  var
boot  dev    home  lib64  media      mnt  opt  root  selinux  sys  usr

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

\time ls
bin  cgroup  etc  lib    lost+found  misc  net  proc  sbin    srv  tmp  var
boot  dev    home  lib64  media      mnt  opt  root  selinux  sys  usr
0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 3600maxresident)k
0inputs+0outputs (0major+261minor)pagefaults 0swaps


the_gripmaster 09-15-2012 12:08 AM

Quote:

Originally Posted by chrism01 (Post 4779946)
There's also the GNU version of time (\time) for more detail eg
Code:

cd /
time ls
bin  cgroup  etc  lib    lost+found  misc  net  proc  sbin    srv  tmp  var
boot  dev    home  lib64  media      mnt  opt  root  selinux  sys  usr

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

\time ls
bin  cgroup  etc  lib    lost+found  misc  net  proc  sbin    srv  tmp  var
boot  dev    home  lib64  media      mnt  opt  root  selinux  sys  usr
0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 3600maxresident)k
0inputs+0outputs (0major+261minor)pagefaults 0swaps


Although I did not get the answer I was looking for but you gave me a big "aha". I didn't know time is also a shell built-in like cd and just executing time uses the shell-builtin and not /usr/bin/time!


All times are GMT -5. The time now is 05:02 AM.