LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 09-12-2012, 10:56 AM   #1
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 356

Rep: Reputation: 38
Question 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?
 
Old 09-12-2012, 03:19 PM   #2
cortman
Member
 
Registered: Jan 2012
Location: ZZ9 Plural Z Alpha
Distribution: Crunchbang 11, LFS 7.3, DSL 4.1.10, Lubuntu 12.10, Debian 7
Posts: 219

Rep: Reputation: 43
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.
 
Old 09-13-2012, 11:49 AM   #3
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 666

Rep: Reputation: 132Reputation: 132
Quote:
Originally Posted by the_gripmaster View Post
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
 
1 members found this post helpful.
Old 09-13-2012, 11:53 AM   #4
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 356

Original Poster
Rep: Reputation: 38
Quote:
Originally Posted by Valery Reznic View Post
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?
 
Old 09-13-2012, 12:38 PM   #5
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 666

Rep: Reputation: 132Reputation: 132
Quote:
Originally Posted by the_gripmaster View Post
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?
 
Old 09-13-2012, 12:47 PM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,533
Blog Entries: 1

Rep: Reputation: 999Reputation: 999Reputation: 999Reputation: 999Reputation: 999Reputation: 999Reputation: 999Reputation: 999
Quote:
Originally Posted by the_gripmaster View Post
]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
 
Old 09-14-2012, 12:43 AM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,226

Rep: Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023
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
 
1 members found this post helpful.
Old 09-14-2012, 11:08 PM   #8
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 356

Original Poster
Rep: Reputation: 38
Quote:
Originally Posted by chrism01 View Post
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!
 
  


Reply

Tags
grep, optimization, sed, time


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sed or grep pgb_710 Programming 20 09-04-2011 12:12 PM
Grep or Sed ? How can I use it ? ArOnaXx Linux - Newbie 6 04-12-2009 02:08 PM
grep -B using sed w3bd3vil Programming 3 08-11-2008 06:37 PM
how to use grep/sed tnik Linux - General 3 11-06-2007 07:51 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM


All times are GMT -5. The time now is 04:55 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration