LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Linux riddle given to me by my teacher. Having problems (https://www.linuxquestions.org/questions/linux-newbie-8/linux-riddle-given-to-me-by-my-teacher-having-problems-4175481313/)

Shodokahn 10-18-2013 01:13 PM

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

grail 10-18-2013 01:18 PM

What do you not understand?

Have you tried using it on a file to see what is displayed?

TB0ne 10-18-2013 01:19 PM

Quote:

Originally Posted by Shodokahn (Post 5048191)
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.

Shodokahn 10-18-2013 01:20 PM

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.

Shodokahn 10-18-2013 01:34 PM

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.

GazL 10-18-2013 01:51 PM

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]"

grail 10-18-2013 02:40 PM

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 :)

GazL 10-18-2013 04:30 PM

Quote:

Originally Posted by grail (Post 5048251)
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 ;).


All times are GMT -5. The time now is 05:29 AM.