Re: Basic Grep Question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 2007-03-13 at 22:04 +0200, Dotan Cohen wrote:
> On 13/03/07, Marc Schwartz <marc_schwartz@xxxxxxxxxxx> wrote:
> > Dan Track wrote:
> > > Hi
> > >
> > > Could someone please tell me how I can grep for two or more different
> > > words in one command instead of piping them through.
> > >
> > > e.g I don't want to do
> > >
> > > cat /tmp/file | grep -v cat | grep -v grey
> > >
> > > I'd like to run that from one grep command.
> > >
> > > Many Thanks
> > > Dan
> >
> > Two possibilities:
> >
> > $ cat .Xresources
> > Emacs.default.attributeBackground:      white
> > Emacs.default.attributeForeground:      black
> > !
> > XEmacs*EmacsFrame.default.attributeForeground: black
> > XEmacs*EmacsFrame.default.attributeBackground: white
> > !
> > xterm*geometry: 100x32
> > xterm*faceName: bitstream vera sans mono
> > xterm*faceSize: 12
> > xterm*background: white
> > xterm*rightScrollBar: true
> >
> >
> > # Use grep with the -E arg to specify extended
> > # regex usage
> >
> > $ cat .Xresources | grep  -Ev "(white|black)"
> > !
> > !
> > xterm*geometry: 100x32
> > xterm*faceName: bitstream vera sans mono
> > xterm*faceSize: 12
> > xterm*rightScrollBar: true
> >
> >
> > # Or just use egrep
> >
> > $ cat .Xresources | egrep  -v "(white|black)"
> > !
> > !
> > xterm*geometry: 100x32
> > xterm*faceName: bitstream vera sans mono
> > xterm*faceSize: 12
> > xterm*rightScrollBar: true
> >
> >
> > You might want to look at:
> >
> >    http://en.wikipedia.org/wiki/Regular_expression
> >    http://www.regular-expressions.info/
> >
> > There is also a great book:
> >
> >    http://regex.info/
> >
> > HTH,
> >
> > Marc Schwartz
> >
> 
> 
> >From Dan's question, it appears that he wants AND behaviour, not OR
> behaviour, from the two greps.
> 
> Dotan Cohen

Dotan,

Keep in mind that he is EXCLUDING lines that have those two values, so
in this case, the use of '|' (OR) gets the desired result. A line cannot
have either value, which is the same as saying that a line cannot have
both values.

If he wanted lines that INCLUDED both values, then the approach would
require the piped double grep approach, considering more complicated
lookahead or lookbehind references, using [g]awk or other alternatives.

One approach, for example, might be to use a Perl based Regex, with
Lookahead. This one will return lines in .Xresources that have BOTH
'xterm' AND 'white' in them:


$ grep -P "(?=.*xterm)(?=.*white)" .Xresources
xterm*background: white


or return lines that have BOTH 'default' AND 'ground':


$ grep -P "(?=.*default)(?=.*ground)" .Xresources
Emacs.default.attributeBackground:      white
Emacs.default.attributeForeground:      black
XEmacs*EmacsFrame.default.attributeForeground: black
XEmacs*EmacsFrame.default.attributeBackground: white


In this way, we can combine multiple regex's in a single grep call.

HTH,

Marc


[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [Fedora Magazine]     [Fedora News]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Maintainers]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Legacy]     [Fedora Desktop]     [Fedora Fonts]     [ATA RAID]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [SSH]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Centos]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Tux]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Asterisk PBX]     [Fedora Sparc]     [Fedora Universal Network Connector]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux