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 03-14-2011, 12:01 PM   #1
bigbada
LQ Newbie
 
Registered: Mar 2011
Posts: 7

Rep: Reputation: 0
regular expression to pattern strings


In a file i have strings:

Code:
- Muammar al-Kaddafi 
- Moammar Gadhafi 
- Muammar al-Qadhafi 
- Mu'ammar Al-Qadhafi 
- Moamer Gadafi
the file is test.txt

Now i wanna write a regular expression pattern to match all the words

I tried
Code:
cat test | grep -r M[uo]'*am+[ae]r\s[aAG]l*-*Q*K*ad+h*afi
But it doesnt work

if i try
cat test | grep -r M[uo] ...till here it works..
Any suggestion ? The regular expression checker online says that the expression is ok.. So whats the problem?

Last edited by bigbada; 03-14-2011 at 12:03 PM.
 
Old 03-14-2011, 12:11 PM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Code:
cat test | grep Muammar al-Kaddafi
cat test | grep Moammar Gadhafi
cat test | grep Muammar al-Qadhafi
cat test | grep Mu'ammar Al-Qadhafi
cat test | grep Moamer Gadafi
 
Old 03-14-2011, 12:16 PM   #3
bigbada
LQ Newbie
 
Registered: Mar 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Im asking for a solution for my regular expression to match all.. What you posted is not my question.
 
Old 03-14-2011, 12:32 PM   #4
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
I know... It just seemed like that's pretty much what you need. I always keep it simple...

But now to your question though... You have only one quotation mark in your expression first of all. Second of all, I do think you need to use egrep.
 
Old 03-14-2011, 12:43 PM   #5
bigbada
LQ Newbie
 
Registered: Mar 2011
Posts: 7

Original Poster
Rep: Reputation: 0
I tried egrep also, but it doesnt find the char ' ...

ab23'afs

So egrep ' doesnt find the word.. Does the ' not work in grep ?
 
Old 03-14-2011, 01:03 PM   #6
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Have you tried escaping the single quotation mark? Using \'
 
Old 03-14-2011, 01:11 PM   #7
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Like said, escape the quotation mark. By itself it may start a quoted piece of characters, but it never stops (no ending quote).

See this for example:
Code:
echo "foo'bar" |grep \'
(GNU grep 2.6.3)

Edit: not that it means anything, but the original post, without explanations, does have a little questionable tone to it, taking into account the current situation. It wouldn't harm if you explained a little more what you're trying to achieve in general; if it's just technical stuff with regular expressions and/or matching in general, you could work this with more general strings (the so-called "minimal working example"). This is not, after all, a political forum.

Last edited by b0uncer; 03-14-2011 at 01:16 PM.
 
Old 03-24-2011, 10:50 AM   #8
jameslynn
LQ Newbie
 
Registered: Mar 2011
Posts: 1

Rep: Reputation: 0
(M[ou]'?am+[ae]r( [AEae]l-?)? ?[GKQ]h?[aeu]+([dtz][dhz]?){1-2}af[iy])
 
Old 03-24-2011, 08:18 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,529

Rep: Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896
Not to be too picky, but without relevance to what you do not want, ultimately the simplest answer (apart from any character on a line as you want all lines),
would be:
Code:
grep '^M' file
 
  


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
regular expression [tT]h* sycamorex Programming 3 01-15-2008 04:43 PM
bourne shell pattern matching or regular expression powah Programming 2 06-30-2006 10:27 AM
Anyone know regular expression? ahhua Linux - Software 1 12-04-2003 08:13 AM
Regular Expression Help WeNdeL Linux - General 1 08-14-2003 10:08 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:49 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