grep : regular expression

guys,

my requirment goes like this:

I have a file, and wish to filter out records where

  1. The first letter is o or O
    and
  2. The next 4 following letter should not be ther

I do not wish to use pipe and wish to do it in one shot.
The best expression I came up with is:

grep ^[oO][^tT][^hH][^eE][^rR]* test1

But with this there is a problem if the word is beginning with o and less then 4 letters it excludes those.

Thanks in advance to the R.E gurus :stuck_out_tongue:

rishi

Did you try

grep -v -i 'other' test1

vino

Thanks Vino for suggestion, but this will not satisfy my requirment:

say my file has

one
only
fine
other
clear
output
o

the result of search should be
one
only
output
o

suppose if i go the way you suggest it will also include "clear" i.e the word not beginning with "o"

hope this sample clarifies the question

rishi

So your list will only contain words and not lines/sentences ?

Can be both ways :smiley:

grep -v -i 'other' test1 | grep '^[oO]'

If running 2 greps is acceptable

[~/temp]$ cat rishi.txt 
one 
only 
fine
other 
clear
output
o
[~/temp]$ sed -n -e '/[oO][tT][hH][eE][rR]/d' -e '/[oO]/p' rishi.txt 
one 
only 
output
o

Will print lines having an o or O.

or

awk '/^[oO]/ && ! /^.[Tt][Hh][Ee][Rr]/{ print;}' test1

should work too.

how abt this

sed '/[Oo][Tt][Hh][Ee][Rr]/d; /^[Oo]/!d' <file>

grep -viE "^[^o]|^other" file

or

egrep -viE "^[^o]|^other" file

depending on your system

Thanks Unix Gurus!!!

U ALL R GENIUS :smiley: