Register
It is currently Sun Apr 20, 2014 10:03 am

Or statement in grep?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Wed Apr 06, 2011 2:44 am   

Joined: Sun Apr 03, 2011 5:58 am
Posts: 7
Hello,

The file:

Code:
Hello
google
lol
haha


If i wanna grep every line in that file that contains either "ha" or "he" i would get the line 1 and 4. How do i manage to do that with one grep command?

Example:

Code:
grep 'ha | he' file.txt


This doesent work but you get my point!

Thanks in advance!


Top
 Profile  
 PostPosted: Wed Apr 06, 2011 2:49 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
You are almost there:
Code:
grep 'ha\|He' file.txt

mind the case, or make it case insensitive:
Code:
grep -i 'ha\|he' file.txt

If you want to avoid the backslash, use extended regular expressions:
Code:
grep -Ei 'ha|he' file.txt

But it can still be written better as
Code:
grep -i 'h[ae]' file.txt


Top
 Profile  
 PostPosted: Wed Apr 06, 2011 8:54 am   

Joined: Sun Apr 03, 2011 5:58 am
Posts: 7
Ah thanks!

So by using "grep -e". | means OR instead of searching for | signs?


Top
 Profile  
 PostPosted: Tue Apr 12, 2011 9:47 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
Generally, yes. -E swaps some special characters, like | and \|, () and \(\), and so on.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP