SunQuest
           UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old October 6th, 2004, 05:30 PM
html-bonehead html-bonehead is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 29 html-bonehead User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 37 m 44 sec
Reputation Power: 0
help with grep

Is there an easy way to consolidate the 2 grep parts of the following bash command into a single grep command?:

cat MyFile | grep 'Blah Blah' | grep xyz$

I'd like to not use the pipe re-direct, but instead tell grep, in one command, to grab any lines in 'MyFile' that meet both of these criteria:

1) contain the string 'Blah Blah' (with the space)

AND

2) end with the string 'xyz.'

I know you can use the '|' (pipe) command to separate 'or' statements in grep, but is there an 'and' statement separator?

Thanks!

Reply With Quote
  #2  
Old October 7th, 2004, 01:34 AM
Muthukumar_K Muthukumar_K is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 13 Muthukumar_K User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to Muthukumar_K
WE can not having an option on grep to use AND there.

But your way of try as,

cat MyFile | grep 'Blah Blah' | grep xyz$ is not efficient.

cat <filename> | grep ... will take more time to do operation.

We can simply this as,

grep -E 'blah blah' filename | grep -E 'xyz$'

or

grep -E 'blah blah' < filename | grep -E 'xyz$'

We can use awk for this too as,

awk ' /blah blah/ { print $0 }' <inputfile | awk ' /xyz$/ { print $0 }'

HTH.

Regards
Muthukumar.

Reply With Quote
  #3  
Old October 7th, 2004, 07:48 AM
jlk's Avatar
jlk jlk is offline
Perl Jedi
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: New Hampshire
Posts: 108 jlk User rank is Sergeant (500 - 2000 Reputation Level)jlk User rank is Sergeant (500 - 2000 Reputation Level)jlk User rank is Sergeant (500 - 2000 Reputation Level)jlk User rank is Sergeant (500 - 2000 Reputation Level)jlk User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 10 h 41 m 53 sec
Reputation Power: 15
Quote:
Originally Posted by html-bonehead
Is there an easy way to consolidate the 2 grep parts of the following bash command into a single grep command?:

cat MyFile | grep 'Blah Blah' | grep xyz$

I'd like to not use the pipe re-direct, but instead tell grep, in one command, to grab any lines in 'MyFile' that meet both of these criteria:

1) contain the string 'Blah Blah' (with the space)

AND

2) end with the string 'xyz.'

I know you can use the '|' (pipe) command to separate 'or' statements in grep, but is there an 'and' statement separator?

Thanks!



Unfortunately grep only takes on pattern for its options so you would need to use the pipe no matter what. What's wrong with pipe? Something against it?

Regards,

jlk

Reply With Quote
  #4  
Old October 7th, 2004, 10:54 AM
html-bonehead html-bonehead is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 29 html-bonehead User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 37 m 44 sec
Reputation Power: 0
nope - just thought maybe there was a cleaner way to do it. Thanks for the reply.

Quote:
Originally Posted by jlk
Unfortunately grep only takes on pattern for its options so you would need to use the pipe no matter what. What's wrong with pipe? Something against it?

Regards,

jlk

Reply With Quote
  #5  
Old October 9th, 2004, 11:07 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 44 m 45 sec
Reputation Power: 9
why not use the rigth tool?
instead of the s....d
cat MyFile | grep 'Blah Blah' | grep xyz$
try
sed -n '/.*Blah Blah.*xyz$/p' MyFile

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > help with grep


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway