LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-18-2013, 02:13 PM   #1
Shodokahn
LQ Newbie
 
Registered: Oct 2013
Posts: 3

Rep: Reputation: Disabled
Linux riddle given to me by my teacher. Having problems


The famous quote from Shakespeare's Hamlet, "To be or not to be," can be represented by the following regular expression:

(2b | [^b] {2})

IF you used this expression when searching a text file using grep and egrep command, what would be displayed? example- (egrep " 2b | [^b] {2})" myfile.txt

WHY does it display what it does, that is the question!?

can someone help me figure this out. Thank you
 
Old 10-18-2013, 02:18 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
What do you not understand?

Have you tried using it on a file to see what is displayed?
 
Old 10-18-2013, 02:19 PM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,961

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by Shodokahn View Post
The famous quote from Shakespeare's Hamlet, "To be or not to be," can be represented by the following regular expression:

(2b | [^b] {2})

IF you used this expression when searching a text file using grep and egrep command, what would be displayed? example- (egrep " 2b | [^b] {2})" myfile.txt
WHY does it display what it does, that is the question!?

can someone help me figure this out. Thank you
Sure...tell us what you're confused about, and what effort you've put into this.

Per the LQ Rules, don't just post verbatim homework questions and ask for them to be answered. We're happy to HELP, but will not do your homework for you.
 
Old 10-18-2013, 02:20 PM   #4
Shodokahn
LQ Newbie
 
Registered: Oct 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Yes I tried it and it puts me into the file that im testing it on. It just shows a flashing cursor under the command I typed. It doesn't do anything else. What is it suppose to show me and why did it do that.
 
Old 10-18-2013, 02:34 PM   #5
Shodokahn
LQ Newbie
 
Registered: Oct 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
I have used my text book and google to look up each individual expression and what they do. For some reason when I type in that command it takes me to a > and displays nothing else. Ive used different commands at the > to try to navigate my file or to exit my file however it does nothing to the file. That's what im confused about. It does nothing and lol I think I just answered my question. Thanks for letting me post. I promise I did my homework on it but just couldn't figure it out. But typing all this I think I know what shes was doing. lol thanks all.
 
Old 10-18-2013, 02:51 PM   #6
GazL
Senior Member
 
Registered: May 2008
Posts: 4,091
Blog Entries: 1

Rep: Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541
If you're getting the > prompt on the command line then the chances are that you've mucked up the quotation marks. Make sure you have matching open and close quotes.

P.S.

Quote:
The famous quote from Shakespeare's Hamlet, "To be or not to be," can be represented by the following regular expression:

(2b | [^b] {2})
I'm not sure that is entirely accurate. Shakespeare's regex would IMO be egrep "2b|[^b]b|b[^b]". When you've worked out your answer I'll tell you why.
And thinking about it, that can be simplified to egrep "[^b]b|b[^b]"

Last edited by GazL; 10-18-2013 at 03:05 PM.
 
Old 10-18-2013, 03:40 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
hmmm ... not so sure GazL ... not 2 b's does not imply that there is at least 1 b, which your solution does?

At OP, I would place single quotes around the regex so the shell also doesn't interpret anything ... not so much an issue here but I have found it a good habit for when you start using asterisks
or question marks
 
1 members found this post helpful.
Old 10-18-2013, 05:30 PM   #8
GazL
Senior Member
 
Registered: May 2008
Posts: 4,091
Blog Entries: 1

Rep: Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541
Quote:
Originally Posted by grail View Post
hmmm ... not so sure GazL ... not 2 b's does not imply that there is at least 1 b, which your solution does?
Ooops, yes your quite right I got myself a little messed up there and missed the possibility that neither was a 'b'. Thanks for the correction grail.

Let's have another go:
echo -e "2b\nbb\nab\nba\naa"| egrep "[^b].|.[^b]" # '2b' or not two 'b's.

The teacher's version works out as " '2b' or two not 'b's " though which isn't Shakespeare either .
 
  


Reply


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Hey there! I'm a Music Teacher using Linux markewooley LinuxQuestions.org Member Intro 0 12-25-2010 09:55 PM
LXer: Linux is a Better Teacher LXer Syndicated Linux News 0 03-25-2010 10:44 AM
LXer: Linux in schools: a teacher speaks LXer Syndicated Linux News 0 07-13-2008 07:30 AM
Linux riddle: How do you install linux on a hp compaq t5000 internet terminal??? cwere4 Linux - General 1 06-28-2007 09:36 AM
Linux is just a Great Teacher vharishankar LinuxQuestions.org Member Success Stories 6 09-26-2004 06:54 AM


All times are GMT -5. The time now is 08:32 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration