Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - General
User Name
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.


  Search this Thread
Old 09-10-2008, 03:28 PM   #1
Registered: Nov 2003
Location: Ontario, Canada
Distribution: Ubuntu
Posts: 269

Rep: Reputation: 30
Problem with simple grep command

For some reason I can't figure out how to get the "or" operator (i.e. "|") to work correctly. Consider the following command:

grep "<title>.*</title>|<p>.*</p>" file.txt

I simply want to get the lines which have the title and paragraph tags. However, the command will not work unless I do one at a time:

grep "<title>.*</title>" file.txt (works!)
grep "<p>.*</p>" file.txt (works!)

grep "<title>.*</title>|<p>.*</p>" file.txt (doesn't work!)
grep "<title>.*</title>\|<p>.*</p>" file.txt (doesn't work!)

Can anyone see what I'm doing wrong? (I'm new to grep btw).

Old 09-10-2008, 03:36 PM   #2
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
use the '-E' option. Extended regular expressions:
grep  -E "<title>.*</title>|<p>.*</p>" test.txt
Old 09-10-2008, 03:37 PM   #3
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
The logical or is part of extended regular expressions, for which you should use egrep.

*edit* or the -E option, as crc321 pointed out.
Old 09-10-2008, 06:01 PM   #4
Registered: Nov 2003
Location: Ontario, Canada
Distribution: Ubuntu
Posts: 269

Original Poster
Rep: Reputation: 30
Thanks everyone!


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
simple read $a / grep question sycamorex Programming 2 01-08-2008 01:07 PM
problem with grep command thodo Linux - Newbie 1 10-25-2007 05:20 AM
Simple Linux command Problem GUIPenguin Linux - Newbie 2 09-23-2004 05:47 PM
Simple grep command problem acis Linux - General 2 07-16-2004 01:21 PM
Simple grep question - I hope ! MiguelE Linux - Newbie 3 04-16-2004 09:46 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:22 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration