Topic: How do ~you~ grep multiple strings

This just came up in a casual email with a friend.  We spoke of the line, "In Unix, there's always more than one way to do something," and its corollary, "and someone is sure to think your way is stupid."

The conversation was about grep.  In FreeBSD,  if one does grep "foo|bar" myfile it doesn't work.  You have to either use egrep or grep -E.    Linux doesn't seem to require that.

There is also of course, grep -e "foo" -e "bar" myfile.

I find that I usually do grep -E---I forget to type egrep until I've typed the the "p" of grep, so it's too late to do egrep--therefore, I use -E.  This also works by the way

grep "foo\|bar" myfile

I suspect it has to do with our typing habits and what's easiest for an individual to type.  So what do you folks use to grep multiples strings?

Inquiring minds (i.e, those who need a life) want to know.

<@andre> i would be so much more efficient if i wasn't so stupid

Re: How do ~you~ grep multiple strings

Hm I admit I never do that. However, I often find myself inverse-grepping something to reduce the amount of displayed results; the command line then ends up looking like this:

$ grep foo somefile | grep -v bar | grep -v baz | grep -v bleh

This isn't as bad as it looks thanks to shells with history browsing smile

Re: How do ~you~ grep multiple strings

and for the lazy among us, like myself

alias grep='egrep' smile

... and then they said "I bet you can't make MINIX kernel panic!!!" And that's when I got mad

Re: How do ~you~ grep multiple strings

LOLOL.  That was a dooooooooooooooooooooooh.   smile  I dunno why I never thought of it.

Err, maybe because I'm stupid.

<@andre> i would be so much more efficient if i wasn't so stupid

Re: How do ~you~ grep multiple strings

yep, definately

very stupid

Re: How do ~you~ grep multiple strings

after all, americans are stupid

harharhar

Re: How do ~you~ grep multiple strings

Gee, thanks guys.

<@andre> i would be so much more efficient if i wasn't so stupid

Re: How do ~you~ grep multiple strings

Do an ls -li on a Linux system to see if grep and egrep and fgrep are actually separate programs, or if they are all just hardlinks to the same program whose operation changes depending on the value of argv[0] (the name it is called as).

Then do the same on a FreeBSD system.  See if things are actually different or not.

Re: How do ~you~ grep multiple strings

Dunno about FreeBSD or Linux but on NetBSD and OpenBSD they are all links to the same binary.

NetBSD
{logos}$ ls -li grep egrep fgrep
806413 -r-xr-xr-x  3 root  wheel  95314 Mar 23  2005 egrep*
806413 -r-xr-xr-x  3 root  wheel  95314 Mar 23  2005 fgrep*
806413 -r-xr-xr-x  3 root  wheel  95314 Mar 23  2005 grep*

OpenBSD
{kernelpanicked}$ ls -li grep egrep fgrep
329785 -r-xr-xr-x  6 root  bin  22.1K Aug 27 10:29 egrep
329785 -r-xr-xr-x  6 root  bin  22.1K Aug 27 10:29 fgrep
329785 -r-xr-xr-x  6 root  bin  22.1K Aug 27 10:29 grep

Last edited by KernelPanicked (2005-09-30 02:33:19)

... and then they said "I bet you can't make MINIX kernel panic!!!" And that's when I got mad

Re: How do ~you~ grep multiple strings

good thinking, phoenix.

Re: How do ~you~ grep multiple strings

It turns out that they are the same in FreeBSD as well.


1224761 -r-xr-xr-x  9 root  wheel  77448 Sep 17 18:16 /usr/bin/egrep
1224761 -r-xr-xr-x  9 root  wheel  77448 Sep 17 18:16 /usr/bin/grep

However, if you do grep "foo|bar" you will get nothing.  If you do grep "foo\|bar", grep -E "foo|bar" or egrep "foo|bar" you get both.   <shrug>

That's interesting though, and as always thanks phoenix.  I had always thought they were different programs sharing nothing but a man page. 

(fgrep turns out to be the same program as well, but I expected that after doing ls -li on grep and egrep.)

<@andre> i would be so much more efficient if i wasn't so stupid

Re: How do ~you~ grep multiple strings

From `man grep` on FreeBSD :

"In basic regular expressions the metacharacters ?, +, {, |,  (,  and  )
lose  their  special  meaning; instead use the backslashed versions \?,
\+, \{, \|, \(, and \)."

Keep Smiling

Re: How do ~you~ grep multiple strings

KernelPanicked wrote:

Dunno about FreeBSD or Linux but on NetBSD and OpenBSD they are all links to the same binary.

NetBSD
{logos}$ ls -li grep egrep fgrep
806413 -r-xr-xr-x  3 root  wheel  95314 Mar 23  2005 egrep*
806413 -r-xr-xr-x  3 root  wheel  95314 Mar 23  2005 fgrep*
806413 -r-xr-xr-x  3 root  wheel  95314 Mar 23  2005 grep*

OpenBSD
{kernelpanicked}$ ls -li grep egrep fgrep
329785 -r-xr-xr-x  6 root  bin  22.1K Aug 27 10:29 egrep
329785 -r-xr-xr-x  6 root  bin  22.1K Aug 27 10:29 fgrep
329785 -r-xr-xr-x  6 root  bin  22.1K Aug 27 10:29 grep

FreeBSD
$ ls -li /usr/bin/grep /usr/bin/egrep /usr/bin/fgrep
23794 -r-xr-xr-x  9 root  wheel  73788 Oct  9 01:46 /usr/bin/egrep
23794 -r-xr-xr-x  9 root  wheel  73788 Oct  9 01:46 /usr/bin/fgrep
23794 -r-xr-xr-x  9 root  wheel  73788 Oct  9 01:46 /usr/bin/grep

but even if these binaries got the same inode, grep and egrep don't have the same
behaviour, so to get the same output, grep -E or egrep 'pattern(s)' item.

sorry, for my weak English.

no more pain...