using grep with | (logical or)

using grep with | (logical or)

Post by Troy D. Goodso » Thu, 20 Feb 1997 04:00:00



What is the proper syntax for a 'logical or' search with grep?

I've tried two different ways with two different results!

1) grep "A \| B" file
did what I wanted

2) grep "B \| A" file
only gave matches with B

What's going on?

Thanks,

Troy

--

>--------- -= my opinions are not necessarily those of jpl =- --------

>http://www.jpl.nasa.gov/ - http://www.csun.edu/~kg46825/TGoodson.html

-----------------------------------------
here are the details (my two searches and system info):

% grep "DVMAG \| CONV" fast.out
0 ITERATION NUMBER =  1     CONVERGENCE STATUS FLAG = -1     FNORM=  .18408D+04
0 ITERATION NUMBER =  2     CONVERGENCE STATUS FLAG = -1     FNORM=  .12396D+03
0 ITERATION NUMBER =  3     CONVERGENCE STATUS FLAG = -1     FNORM=  .84334D-03
0 CONVERGENCE STATUS FLAG =   1     CONVERGENCE HAS     BEEN ACHIEVED
0 DX .273593035498D-04 DY .582474224107D-04 DZ .298879097977D-04   DVMAG .709547803907D-04   MASS .100000000000D+04
0 ITERATION NUMBER =  1     CONVERGENCE STATUS FLAG = -1     FNORM=  .10010D+06
0 ITERATION NUMBER =  2     CONVERGENCE STATUS FLAG = -1     FNORM=  .72441D+05
0 ITERATION NUMBER =  3     CONVERGENCE STATUS FLAG = -1     FNORM=  .64016D+04
0 ITERATION NUMBER =  4     CONVERGENCE STATUS FLAG = -1     FNORM=  .29393D+03
0 ITERATION NUMBER =  5     CONVERGENCE STATUS FLAG = -1     FNORM=  .46429D+00
0 CONVERGENCE STATUS FLAG =   1     CONVERGENCE HAS     BEEN ACHIEVED
0 DX .151526323260D+00 DY-.112400484139D+00 DZ-.628783794066D-02   DVMAG .188768727233D+00   MASS .100000000000D+04
0 DX .200552713084D+00 DY-.348479148487D+00 DZ-.131365132452D+00   DVMAG .422984521797D+00   MASS .100000000000D+04
0 ITERATION NUMBER =  1     CONVERGENCE STATUS FLAG = -1     FNORM=  .11259D+08
0 ITERATION NUMBER =  2     CONVERGENCE STATUS FLAG = -1     FNORM=  .80191D+07
0 ITERATION NUMBER =  3     CONVERGENCE STATUS FLAG = -1     FNORM=  .77603D+07
0 ITERATION NUMBER =  4     CONVERGENCE STATUS FLAG = -1     FNORM=  .10634D+07
0 ITERATION NUMBER =  5     CONVERGENCE STATUS FLAG = -1     FNORM=  .29911D+06
0 ITERATION NUMBER =  6     CONVERGENCE STATUS FLAG = -1     FNORM=  .33830D+03
0 ITERATION NUMBER =  7     CONVERGENCE STATUS FLAG = -1     FNORM=  .86701D-02
0 CONVERGENCE STATUS FLAG =   1     CONVERGENCE HAS     BEEN ACHIEVED
0 DX-.180667280101D+00 DY .140636986436D+00 DZ .253990921772D-01   DVMAG .230357422143D+00   MASS .100000000000D+04
0 DX-.180667280101D+00 DY .140636986436D+00 DZ .253990921772D-01   DVMAG .230357422143D+00   MASS .100000000000D+04

% grep "CONV \| DVMAG" fast.out
0 DX .273593035498D-04 DY .582474224107D-04 DZ .298879097977D-04   DVMAG .709547803907D-04   MASS .100000000000D+04
0 DX .151526323260D+00 DY-.112400484139D+00 DZ-.628783794066D-02   DVMAG .188768727233D+00   MASS .100000000000D+04
0 DX .200552713084D+00 DY-.348479148487D+00 DZ-.131365132452D+00   DVMAG .422984521797D+00   MASS .100000000000D+04
0 DX-.180667280101D+00 DY .140636986436D+00 DZ .253990921772D-01   DVMAG .230357422143D+00   MASS .100000000000D+04
0 DX-.180667280101D+00 DY .140636986436D+00 DZ .253990921772D-01   DVMAG .230357422143D+00   MASS .100000000000D+04

% uname -a
HP-UX A.09.05 A 9000/725

% grep -V
GNU e?grep, version 1.6

 
 
 

using grep with | (logical or)

Post by Tad McClell » Fri, 21 Feb 1997 04:00:00


[ What's the idea with the funky settings for Followups?
  I changed them back to normal.
]


: What is the proper syntax for a 'logical or' search with grep?

: I've tried two different ways with two different results!

: 1) grep "A \| B" file
: did what I wanted

: 2) grep "B \| A" file
: only gave matches with B

: What's going on?
  ^^^^^^^^^^^^^^^

You are:

i) not using 'egrep' which understands 'or's

ii) _escaping_ the or so that it _won't_ be an or!

iii) spaces matter. Too much data for me to try and figure out
     if you really want to match  'DVMAG ' or ' CONV'

: % grep "DVMAG \| CONV" fast.out

egrep 'DVMAG|CONV' fast.out

--
    Tad McClellan                          SGML Consulting
    Tag And Document Consulting            Perl programming


 
 
 

using grep with | (logical or)

Post by Phil Jense » Fri, 21 Feb 1997 04:00:00




> : What is the proper syntax for a 'logical or' search with grep?
> : I've tried two different ways with two different results!
> : 1) grep "A \| B" file
> : did what I wanted
> : 2) grep "B \| A" file
> : only gave matches with B

Grep's default action is to search for basic regular expressions,
which doesn't support the | character.  On the other hand, if you
invoke egrep, or grep -E (extended), you should be ok.  

Quote:> iii) spaces matter. Too much data for me to try and figure out
>      if you really want to match  'DVMAG ' or ' CONV'

Yup.

-p

 
 
 

1. How to make grep select both lines using only a single 'grep' command

: Hello,
:       We have an ASCII file called "ships" with lines in this format:

:       .....      3HO/3HA(PNTN) 4SD .....       [line 1]
:       .....      3HO/3HA 4SD .....             [line 2]
:       .                                        [line n]

:       The stuff in the parenthesis (the string "PNTN") can change.

:       Is there a single grep/egrep regular expression that can display
:       both lines?

:       I tried:

:       1. grep -i "3ho/3ha * 4s" ships,    showed line 2 only
:       2. grep -i "3ho/3ha* 4s" ships ,    showed line 2 only
:       3. grep -i "3ho/3ha *4s" ships ,    showed line 2 only
:       4. grep -i "3ho/3ha(*) 4s" ships,   showed nothing
:       5. grep -i "3ho/3ha(*) 4s" ships,   showed nothing
:       6. grep -i "3ho/3ha(p*) 4s" ships,  showed nothing

Remember that regular expressions and filename globbing are different things.
You were attempting filename globbing, when grep requires a regular expression.
Try this:

grep -i "3ho/3ha.* 4s" grepp

The ".*" does what you were trying to do with the "*", which is match a string
of 0 or more characters.

Beirne

--
Beirne Konarski         | Subscribe to the Unicycling Mailing List

Kent State University   | Unicycling Web Page:
"Untouched by Scandal"        | http://nimitz.mcs.kent.edu/~bkonarsk/

2. ssh from one machine, but not from another ...

3. Using grep -v and grep -C together

4. less / xterm

5. grep sTerm * | grep -v grep ???

6. PCI modem for OS/2 and Linux?

7. tail | grep | grep | grep

8. Another Newbie Question

9. How do I grep for more than one pattern (logical AND).

10. grep: logical AND across multiple files?

11. Grep regular expression pattern (logical AND)

12. using logical drive of MS-DOS extended partition for Linux

13. Using DiskSuite ufs logging on Volume Manager logical volumes?