Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
cancel
Showing results for 
Search instead for 
Did you mean: 

grep and "AND"

SOLVED
Go to solution
Balaji N
Honored Contributor

grep and "AND"

hi gurus
am stuck with this for quite some time now. googling doesnt help.


let us say my file is like this
*****************************
#a
abcd
#abcd
abcd1
*******************************

i want to find out all occurances of abcd which doesnt have this comment.

i know this

grep -v ^# file | grep "abcd" file.

but is it possible to do this in a single grep.

something like this

grep -e (^#|abcd) file

will matach either of this pattern.

is there something will match both the pattern (the AND clause).

thanks for all the help. am stuck at this moment and bunnies for quick answers.
-balaji

Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
7 REPLIES
Steven E. Protter
Exalted Contributor

Re: grep and "AND"

grep abcd file.text | grep -v ^#

output:

abcd
abcd1

worked for me in HP-UX?

What about Linux? Or do I not understand the question?

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
John Poff
Honored Contributor

Re: grep and "AND"

Hi,

I got this to work:

grep "^abcd" file

but I don't think it is exactly what you are
looking for, as it will fail to find:

# abcd


SEP, the way you mentioned works but Balaji
is looking to do it with a single grep command.

JP
Balaji N
Honored Contributor

Re: grep and "AND"

hi SEP,
i am looking for that to be done in a single grep.

is there a way do it without piping in the output to grep.

-balaji
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
Stuart Browne
Honored Contributor
Solution

Re: grep and "AND"

This command should do it:

grep "^[^#]bcd" file

The [^#] is basically saying "not #".

But the leading ^ (outside of the []'s) will say that it needs to be at the start of the line, but it also takes up a character space.

This brings us to some more regular expression fun:

grep "^[^#]*abcd" file

will match properly as requested. The * obviously meaning '0 or more' of the previous atom (character not # which is at the start of a string)...

yay.. fun.. Ok, so I started in one direction, found the proper one, and couldn't be bothered re-typing the response ;P
One long-haired git at your service...
Steven E. Protter
Exalted Contributor

Re: grep and "AND"

I've tested the final solutions of all contestants and they all work.

I always love Stuart's answers
grep "^[^#]bcd"

output

abcd
abcd1


I knew it wasn't so easy as my answer. It would be nice to know however why two greps are a problem. Overhead? Or just looking for an elegant solution.

Apologies for my misunderstanding. Feel free to zero point both posts.

:-)

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
John Meissner
Esteemed Contributor

Re: grep and "AND"

i'd use

cat file | grep ^abcd

All paths lead to destiny
Wouter Jagers
Honored Contributor

Re: grep and "AND"

sed masters in being one process:

sed -n -e '/^#/d' -e '/abcd/p' < file

regards
Wouter
an engineer's aim in a discussion is not to persuade, but to clarify.