egrep query....request for help


 
Thread Tools Search this Thread
Special Forums UNIX Desktop Questions & Answers egrep query....request for help
# 1  
Old 05-28-2010
egrep query....request for help

Hi

Please can someone help me with a query. Will the following query bring back 'Jolly Brewers'. My understanding is that it will bring back either 'Jolly' or 'Brewers' but not 'Jolly Brewers'....because of the pipe

egrep '(Jolly)|(Brewers)' Public_Houses

Please can someone confirm?

thanks

# 2  
Old 05-28-2010
Maybe this excerpt from the egrep man page will help you to better understand how egrep works, note the sentence that I have underlined:

DESCRIPTION
grep searches the named input FILEs (or standard input if no files are
named, or the file name - is given) for lines containing a match to the
given PATTERN. By default, grep prints the matching lines.


---------- Post updated at 00:54 ---------- Previous update was at 00:50 ----------

Code:
$ egrep '(Jolly)|(Brewers)' public
Jolly Brewers
$ egrep -o '(Jolly)|(Brewers)' public
Jolly
Brewers
$

# 3  
Old 05-28-2010
So I was wrong! I thought the pipe in:

egrep '(Jolly)|(Brewers)' public

meant OR...i.e Jolly OR Brewers....and not both. I guess it must be the brackets that change the meaning slightly

A very similar question comes from Solaris SCSAS exam


Quote:
Originally Posted by pseudocoder
Maybe this excerpt from the egrep man page will help you to better understand how egrep works, note the sentence that I have underlined:

DESCRIPTION
grep searches the named input FILEs (or standard input if no files are
named, or the file name - is given) for lines containing a match to the
given PATTERN. By default, grep prints the matching lines.


---------- Post updated at 00:54 ---------- Previous update was at 00:50 ----------

Code:
$ egrep '(Jolly)|(Brewers)' public
Jolly Brewers
$ egrep -o '(Jolly)|(Brewers)' public
Jolly
Brewers
$

# 4  
Old 05-28-2010
egrep '(Jolly)|(Brewers)' public

In fact in this case the pipe means AND as well as OR.
AFAIK it has nothing to do with the brackets, if you omit the brackets, the result should be the same.
Additionally note the difference between one pipe and two pipes, where the latter means OR in shell.

---------- Post updated at 01:32 ---------- Previous update was at 01:22 ----------

All right, I have to correct myself, one pipe does indeed mean OR
Regular Expression Tutorial (Section "Alternation")
# 5  
Old 05-28-2010
okay, I'm back to square one. The official answer to the question says that the following statement will return 'Jolly Brewers'....but how can it when pipe represents OR?

egrep '(Jolly)|(Brewers)' Public_Houses

The actual Solaris SCSAS question is fractionally different but I don't see how it would make any difference to the answer


Quote:
Originally Posted by pseudocoder
egrep '(Jolly)|(Brewers)' public

In fact in this case the pipe means AND as well as OR.
AFAIK it has nothing to do with the brackets, if you omit the brackets, the result should be the same.
Additionally note the difference between one pipe and two pipes, where the latter means OR in shell.

---------- Post updated at 01:32 ---------- Previous update was at 01:22 ----------

All right, I have to correct myself, one pipe does indeed mean OR
Regular Expression Tutorial (Section "Alternation")
# 6  
Old 05-28-2010
Strange, after some more research, it looks like I initially was right with AND/OR... Finally I'll stick by my initial definition.

A pipe in the egrep pattern stands for a delimiter. A double pipe stands for OR.
However egrep will in both cases print the matching line (which contains both patterns).
Let me illustrate this:
Code:
$ egrep --color=always '(Jolly)|(Brewers)' public
Jolly Brewers
$ egrep --color=always '(Jolly)||(Brewers)' public
Jolly Brewers
$

--colour[=WHEN], --color[=WHEN]
Surround the matching string with the marker find in GREP_COLOR
environment variable. WHEN may be `never', `always', or `auto'
# 7  
Old 05-28-2010
thanks for your help...I will check it out tomorrow...

Funnily enough I tried it out on a Unix box but couldn't get it to return either one.

Quote:
Originally Posted by pseudocoder
Strange, after some more research, it looks like I initially was right with AND/OR... Finally I'll stick by my initial definition.

A pipe in the egrep pattern stands for a delimiter. A double pipe stands for OR.
However egrep will in both cases print the matching line (which contains both patterns).
Let me illustrate this:
Code:
$ egrep --color=always '(Jolly)|(Brewers)' public
Jolly Brewers
$ egrep --color=always '(Jolly)||(Brewers)' public
Jolly Brewers
$

--colour[=WHEN], --color[=WHEN]
Surround the matching string with the marker find in GREP_COLOR
environment variable. WHEN may be `never', `always', or `auto'
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Request: How to Parse dynamic SQL query to pad extra columns to match the fixed number of columns

Hello All, I have a requirement in which i will be given a sql query as input in a file with dynamic number of columns. For example some times i will get 5 columns, some times 8 columns etc up to 20 columns. So my requirement is to generate a output query which will have 20 columns all the... (7 Replies)
Discussion started by: vikas_trl
7 Replies

2. Shell Programming and Scripting

Shell Script to execute Oracle query taking input from a file to form query

Hi, I need to query Oracle database for 100 users. I have these 100 users in a file. I need a shell script which would read this User file (one user at a time) & query database. For instance: USER CITY --------- ---------- A CITY_A B CITY_B C ... (2 Replies)
Discussion started by: DevendraG
2 Replies

3. Shell Programming and Scripting

Query regarding egrep command

Hi All, I am having a query regarding the usage of egrep command. i am having two unix environmanets in environment when i am using "egrep -f" it is working fine and other unix environment i am getting a syntax error. Please let me know if i need to set any environmane variables. ... (12 Replies)
Discussion started by: Sriram.Vedula53
12 Replies

4. Shell Programming and Scripting

Query Oracle tables and return values to shell script that calls the query

Hi, I have a requirement as below which needs to be done viz UNIX shell script (1) I have to connect to an Oracle database (2) Exexute "SELECT field_status from table 1" query on one of the tables. (3) Based on the result that I get from point (2), I have to update another table in the... (6 Replies)
Discussion started by: balaeswari
6 Replies

5. Shell Programming and Scripting

add the output of a query to a variable to be used in another query

I would like to use the result of a query in another query. How do I redirect/add the output to another variable? $result = odbc_exec($connect, $query); while ($row = odbc_fetch_array($result)) { echo $row,"\n"; } odbc_close($connect); ?> This will output hostnames: host1... (0 Replies)
Discussion started by: hazno
0 Replies

6. UNIX for Dummies Questions & Answers

search ")" with egrep - egrep: syntax error

Hi Guys, we have a shell script which basically query the Database which retrieves huge data and use the data with "egrep" . Now there is some data which contains characters like "abc)" and the same is used like below : "egrep (.+\|GDPRAB16\|GDPR/11702 96 abc)\|$ temp.txt" now while... (7 Replies)
Discussion started by: sagarjani
7 Replies

7. UNIX for Dummies Questions & Answers

Egrep cheat sheet anywhere? Looking for meaning of egrep -c

Hi I've been searching google and have not found what egrep -c means. Does anyone know where I can get a cheat sheet or what that -c means? thanks, Linda (2 Replies)
Discussion started by: leelm
2 Replies

8. Shell Programming and Scripting

egrep help

How can i make something like if (echo "$arg2" | egrep -s '^+\.+km/h+$|^+km/h+$'); then not to output the value of $arg2 on the screen, evertime i get match it outputs the value of the variable on the screen which i don't need to do. I know for grep its -q option but it doesn't work for egrep.... (2 Replies)
Discussion started by: Vozx
2 Replies

9. Shell Programming and Scripting

HTTP Query Request & Parsing returned XML output

I have a PERL script from which I need to make a HTTP request to Web Servlet (Essentially a URL with variables and values like &Variable1=AAAAAA&Variable2=BBBBBBBBB&Variable3=CCCCCCC). The Web servlet returns an XML result which needs to be parsed for the contents of the result within the program.... (15 Replies)
Discussion started by: jerardfjay
15 Replies
Login or Register to Ask a Question