LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris
User Name
Password
Solaris / OpenSolaris This forum is for the discussion of Solaris, OpenSolaris, OpenIndiana, and illumos.
General Sun, SunOS and Sparc related questions also go here. Any Solaris fork or distribution is welcome.

Notices


Reply
  Search this Thread
Old 01-21-2008, 11:37 AM   #1
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333

Rep: Reputation: 17
egrep puzzle


Hi,

I was playing with egrep on Solaris 8 and wanted to egrep for either string 1 or string 2 and string 3.

This is the easy way.
egrep 'string1|string2' file | egrep string3

But I wanted to try and do it in 1 egrep, to see if it would work.


I tried this below which did'nt work to my surprise as I am sure it should...

egrep '(string1|string2)string3' file

Thanks for any tips.
 
Old 01-21-2008, 12:34 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Works on my machine....what error are you getting?
 
Old 01-22-2008, 03:52 AM   #3
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333

Original Poster
Rep: Reputation: 17
Nope, does'nt work for me.

$ uname -r
5.8
$ cat -tv z
abcdef 1234/tcp # blah blah
xyz^I9999/tcp^I^I# comment field
$ egrep '(abc|xyz)blah' z
$ egrep '(abc|xyz)comment' z
$ echo $?
1
 
Old 01-22-2008, 04:10 AM   #4
greeneagle
Member
 
Registered: Dec 2004
Location: Dublin Ireland via Cobh, Limerick.
Distribution: RedHat, Suse, Ubuntu.
Posts: 33

Rep: Reputation: 16
Smile

Quote:
Originally Posted by dazdaz View Post
for either string 1 or string 2 and string 3.
...
egrep '(string1|string2)string3' file
well that means grep for string1 or string2 followed by string3, doesn't it?

Do you mean
1. for either string 1 or (string 2 and string 3). ?
or
2. for (either string 1 or string 2) and string 3. ?

echo string1 > file
echo string2 >> file
echo string3 >> file
echo string1string3 >> file
echo string2string3 >> file

2.

egrep '(string1|string2)string3' file
string1string3
string2string3

1.

egrep '(string1|string2.*string3)' file
string1
string1string3
string2string3

What was the question again?

 
Old 01-22-2008, 04:15 AM   #5
greeneagle
Member
 
Registered: Dec 2004
Location: Dublin Ireland via Cobh, Limerick.
Distribution: RedHat, Suse, Ubuntu.
Posts: 33

Rep: Reputation: 16
Smile

I think you want:

$ egrep '(abc|xyz).*blah' z
$ egrep '(abc|xyz).*comment' z

Otherwise it means match abcblah or xyzblah or nothing
.* means match any char (.) 0 or more times (*)

Quote:
Originally Posted by dazdaz View Post
$ cat -tv z
abcdef 1234/tcp # blah blah
xyz^I9999/tcp^I^I# comment field
$ egrep '(abc|xyz)blah' z
$ egrep '(abc|xyz)comment' z
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
egrep question graziano1968 Linux - General 1 05-29-2007 08:28 AM
egrep noobie zendez33 Linux - Newbie 5 05-09-2006 11:59 PM
Help with egrep smart_sagittari Linux - Newbie 2 05-02-2005 08:18 AM
Using Grep and Egrep linux-nerd Linux - General 5 10-10-2004 11:37 AM
Using egrep Barbarian Programming 5 10-20-2002 02:54 PM

LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris

All times are GMT -5. The time now is 11:34 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration