LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 03-23-2011, 01:10 AM   #1
915086731
Member
 
Registered: Apr 2010
Posts: 119
Blog Entries: 4

Rep: Reputation: 1
Question A question about regular expression


Hello.
I have a trouble with regular expression when using "grep" and "awk".Like the following content:
Code:
a fun()
  fun()d
dffun()d
__fun()d
a=fun()
a	fun() (a tab after "a")
I want to find the line, word "fun" with a space or a tab or "=" before.That's say I want to get the following result:
Code:
a fun()
  fun()d
a=fun()
a	fun() (a tab after "a")
Can anyone help me?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-23-2011, 01:37 AM   #2
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie
Posts: 1,429

Rep: Reputation: 83
Hya

Code:
grep '[[:space:]]fun\|=fun'
Happy Penguins!
 
2 members found this post helpful.
Old 03-23-2011, 01:38 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Literally as you said it: word "fun" with a space or a tab or "=" before. Use a character list to include one of these characters. For convenience I'd use the generic pattern [:space:]
Code:
grep '[[:space:]=]fun' file
awk '/[[:space:]=]fun/' file
 
2 members found this post helpful.
Old 03-23-2011, 06:34 AM   #4
915086731
Member
 
Registered: Apr 2010
Posts: 119
Blog Entries: 4

Original Poster
Rep: Reputation: 1
Thanks for the two friends above.
But I do not know why I can't use
Code:
grep  '[= \t]'
If I just want to select word "fun" with a tab or "=" before, and word with space before is not accepted.How can I do?
 
Old 03-23-2011, 07:55 AM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
grep does not accept \t, unless you use the -P option for perl compatible regexps (it is available only in GNU grep, anyway). Therefore you have to use the [:space:] pattern or type a literal TAB using Ctrl-V + TAB:
Code:
grep '[     = ]fun' file
On the other hand, awk and sed (and obviously perl) interpret \t correctly:
Code:
awk '/[\t= ]fun/' file
sed -n '/[\t= ]fun/p' file
 
1 members found this post helpful.
  


Reply

Tags
regular expressions


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expression Question yuye811 Programming 7 06-19-2009 01:55 AM
Regular Expression Question supasharp Linux - General 3 06-12-2007 12:58 PM
Regular Expression Question windisch Programming 8 05-22-2007 03:27 PM
Regular expression question. groentebroer Programming 2 11-29-2004 09:15 PM
regular expression question Gantrep Linux - Software 2 04-20-2003 04:24 PM


All times are GMT -5. The time now is 12:28 PM.

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