LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-25-2007, 11:26 AM   #1
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
grep inclusive and


hi please check out this example:
Code:
schneidz@lq temp> cat temp.txt
hello
hello world
world
schneidz@lq temp> egrep "(hello|world)" temp.txt
hello
hello world
world
matches all three lines because they have hello or world in it.

i would like only the line that has hello and world in it.
Code:
schneidz@lq temp> grep hello*world temp.txt
produces nothing. i like grep because i can use the '-l' switch which returns the filename only.

this is on aix so some options are missing from the gnu version

thanks,

Last edited by schneidz; 09-25-2007 at 11:29 AM.
 
Old 09-25-2007, 11:35 AM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Try:
grep 'hello.*world' temp.txt

Don't forget the single quotes. You don't want your shell expanding values for you...
 
1 members found this post helpful.
Old 09-25-2007, 11:40 AM   #3
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Original Poster
Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
thanks so much.

fyi this works too:
Code:
egrep hello.+world temp
 
Old 09-25-2007, 05:02 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
The example above will get the lines provided, but would miss "world hello". For this there are several options, of which I present two:
Code:
grep hello temp |grep world
egrep '(hello.*world|world.*hello)'
 
1 members found this post helpful.
Old 09-25-2007, 08:31 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
use awk. it gives you more flexibility than (e)grep.
Code:
awk '/hello/ && /world/' "file"
output:
Code:
# awk '/hello/ && /world/' "file"
hello world
 
1 members found this post helpful.
  


Reply



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
New Linux Does Inclusive Virtualization DragonSlayer48DX Linux - News 0 04-27-2007 06:17 PM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 04:56 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
One all-inclusive simple way to install OpenGL? madonid Linux - Newbie 1 01-21-2006 01:43 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:50 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
Open Source Consulting | Domain Registration