LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-03-2009, 10:46 PM   #1
shogun1234
Member
 
Registered: May 2004
Posts: 165

Rep: Reputation: 15
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.
 
Old 10-03-2009, 11:07 PM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
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.

Last edited by slakmagik; 10-03-2009 at 11:09 PM. Reason: recalled gnuism
 
Old 10-03-2009, 11:09 PM   #3
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by shogun1234 View Post
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
 
Old 10-03-2009, 11:10 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,053

Rep: Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971
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
 
Old 10-03-2009, 11:13 PM   #5
kirukan
Senior Member
 
Registered: Jun 2008
Location: Eelam
Distribution: Redhat, Solaris, Suse
Posts: 1,217

Rep: Reputation: 131Reputation: 131
Try this
cat target | egrep "(from|to)"Message
 
Old 10-04-2009, 03:43 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,524
Blog Entries: 27

Rep: Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175
Quote:
Originally Posted by syg00 View Post
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.
 
Old 10-04-2009, 04:08 AM   #7
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
That would also match 'from', if it was in the data, though.

Last edited by slakmagik; 10-04-2009 at 04:09 AM. Reason: be clearer
 
Old 10-04-2009, 04:19 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,524
Blog Entries: 27

Rep: Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175
Quote:
Originally Posted by slakmagik View Post
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
 
Old 10-04-2009, 04:29 AM   #9
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Sure thing. But
Code:
egrep '(from|to)Message' target
would also work.
 
Old 10-04-2009, 06:58 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,524
Blog Entries: 27

Rep: Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175
Quote:
Originally Posted by slakmagik View Post
Sure thing. But
Code:
egrep '(from|to)Message' target
would also work.
And so much neater, too
 
Old 10-10-2009, 09:17 AM   #11
shogun1234
Member
 
Registered: May 2004
Posts: 165

Original Poster
Rep: Reputation: 15
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.: )
 
  


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
Search for some pattern and then delete N records after it? Passions Programming 3 11-12-2008 01:23 PM
vim search pattern Berticus General 3 08-21-2008 12:13 PM
Question on Aptitude search pattern Akhran Debian 1 02-11-2006 04:18 AM
Question on aptitude search pattern Akhran Linux - Newbie 4 02-10-2006 12:39 PM
Pattern search in a line jitz Linux - General 2 12-06-2003 04:50 AM


All times are GMT -5. The time now is 09:12 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration