Grep questions

Hello All,

I have few of questions related to Grep given below:

1. Like Perl, is it possible in Grep to negate characters in square brackets. For example in Perl, if '^' is used inside '' then it acts as a negation characters. Can same be achieved through Grep's regular expression.

  1. How do we match special characters in Grep?

  2. I have following file:
    cat /tmp/pk.ldif

My aim is to find only those files starting with dn, and containing the string 'ou=' exactly 3 times. Can anybody why this is not working:

Regular expressions with grep are platform dependent.

Some versions of grep will not accept a regular expression unless you set a switch instructing grep that a regex is used.

For example, on my desktop Mac OSX from the grep man page:

       -G, --basic-regexp
              Interpret  PATTERN  as  a  basic regular expression (see below).
              This is the default.

So, you need to look at your version of grep and find out what flags to set to use a regex (or maybe it is your default and you don't need a switch).

How about your version of grep?

FYI, on my Mac:

apple:~ neo$ egrep -i "^dn:(ou=[[:alnum:]]+,){3}" /tmp/pk.ldif 
dn:ou=76884580,ou=Company,ou=Personal,o=paragkalra.com
dn:ou=People,ou=76884580,ou=Company,ou=Personal,o=paragkalra.com
dn:ou=Groups,ou=76884580,ou=Company,ou=Personal,o=paragkalra.com

Yes, and this is true of probably all regular expressions. The syntax is nearly identical across all platforms; however there may be variations on handling special characters such as the dash (-), the brackets ([ and ]) and the caret (^) itself. Character classes, such as [[:digit:]] work too, but there are slight differences.

You escape them with the backslash (\), as you usually do in perl. The problem is that in the normal grep mode, some special characters ^ $ . * are always on, and some ( ) ? + { } are on when you escape them. However, if you use "egrep" (or -E with GNU grep), you get the situation where all special characters are "magical" unless you escape them.

Yeah, because grep treats each separate line as a new search space. So the whole idea of matching a set of 3 lines with (atom){3} won't work, period. So grep is not the right tool here unless you first use tr or sed to translate all newlines into some other caracter (x1B for instance). Then you need to use egrep or grep -E and your regexp *might* work.

Thanks to all...I was able to work out the negation as well
EG:

I am also running GNU grep. I could not get your last solution to work.

Please post your exact command from the command line and (correct) output.

Thanks.

If awk is allowed :slight_smile:

awk '/^dn/ && gsub("ou=","ou=")==3' file

Ok using a better example:

My aim is to print the rows with 3 ous and 3rd ou should not contain a comma. Following is the sample file:

# cat ou_comma_eg.ldif

So I executed
# grep -i '^dn:\(ou=[^,]\+,\)\{1\}ou=Company,ou=Personal,o=paragkalra.com' ou_comma_eg.ldif

As you can see first 2 rows were not printed as it contained a comma and last 2 rows were not printed as it has 4 ous. Hence only 3rd row was printed.

Ah! OK!

That is different than your first requirement (in your first post on this topic). Now I understand :smiley: