LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Pattern search question in shell (http://www.linuxquestions.org/questions/linux-newbie-8/pattern-search-question-in-shell-759516/)

shogun1234 10-03-2009 11:46 PM

Pattern search question in shell
 
I read a webpage - http://promberger.info/linux/2009/01...shell-scripts/

and would like to search functions which contains fromMessage and toMessage.

Then I try the command
Code:

cat target | grep \(from\|to\)Message
This would cause nothing to return

Code:

cat target | grep (from|to)Message
This would cause "bash: syntax error near unexpected token `from'"


Code:

cat target | grep \[from\|to\]Message
This would return

Code:

fromMessage
toMessage
PersistMessage

What function I need to use so that I can get the following result?

Code:

fromMessage
toMessage




Below is the target file content:
Code:

fromMessage

toMessage


abcMessage

Message

PersistMessage


Thanks for help.

slakmagik 10-04-2009 12:07 AM

You need to quote parentheses from the shell and use egrep for alternatives and don't need cat:

egrep '(from|to)Message' target

-- Well, you can use

grep '\(from\|to\)Message' data

but I don't recommend it. A lot easier to prefix 'e' or use the extended switch and be more standard at the same time.

lutusp 10-04-2009 12:09 AM

Quote:

Originally Posted by shogun1234 (Post 3706726)
I read a webpage - http://promberger.info/linux/2009/01...shell-scripts/

and would like to search functions which contains fromMessage and toMessage.

Then I try the command
Code:

cat target | grep \(from\|to\)Message
This would cause nothing to return

Code:

cat target | grep (from|to)Message
This would cause "bash: syntax error near unexpected token `from'"


Code:

cat target | grep \[from\|to\]Message
This would return

Code:

fromMessage
toMessage
PersistMessage

What function I need to use so that I can get the following result?

Code:

fromMessage
toMessage




Below is the target file content:
Code:

fromMessage

toMessage


abcMessage

Message

PersistMessage


Thanks for help.

Do it this way:

Code:

$ grep -P "(from|to)Message" < target
And read:

Code:

$ man grep

syg00 10-04-2009 12:10 AM

Your first one is closest to working - try it as
Code:

grep "\(from\|to\)"Message target
- note "cat" not required.
Better is to use egrep and avoid the escapes altogether -
Code:

grep -E "(from|to)"Message target

kirukan 10-04-2009 12:13 AM

Try this
cat target | egrep "(from|to)"Message

catkin 10-04-2009 04:43 AM

Quote:

Originally Posted by syg00 (Post 3706747)
Your first one is closest to working - try it as
Code:

grep "\(from\|to\)"Message target
- note "cat" not required.
Better is to use egrep and avoid the escapes altogether -
Code:

grep -E "(from|to)"Message target

All good but single quotes are the best choice because nothing (absolutely nothing) between the opening and closing single quotes is touched by the shell. Leaves you with this as the most minimal solution
Code:

egrep '(from)|(to)Message' target
Good practice (saves brain work and complexifusion): use single quotes whenever you don't need double quotes.

slakmagik 10-04-2009 05:08 AM

That would also match 'from', if it was in the data, though.

catkin 10-04-2009 05:19 AM

Quote:

Originally Posted by slakmagik (Post 3706944)
That would also match 'from', if it was in the data, though.

Oops! So it would and thank you for pointing it out. This is better
Code:

egrep '((from)|(to))Message' target

slakmagik 10-04-2009 05:29 AM

Sure thing. :) But
Code:

egrep '(from|to)Message' target
would also work.

catkin 10-04-2009 07:58 AM

Quote:

Originally Posted by slakmagik (Post 3706971)
Sure thing. :) But
Code:

egrep '(from|to)Message' target
would also work.

And so much neater, too :)

shogun1234 10-10-2009 10:17 AM

I read the manual and at the moment using
Code:

grep -E '(from|to)'Message text.txt
looks working fine for me.

Thanks all your help.

Those information are really helpful.

I appreciate it.: )


All times are GMT -5. The time now is 03:14 PM.