grep question

Hello all,

Is grep treating special the fallowing characters: or ] cause I made a grep with a string that contains ]say:

grep ‘<</asasa[2313]sasa’ file and I didn’t find nothing but if I grep with:
grep '<</asasa file
it finds that line and the line contains ] characters.

] are used to enclose a character class. [abc] means any of the characters a, b, or c in that position. And you can guess that [0-9] means any digit. If you wanted a literal you should precede it with .

The metacharacters that you should watch out for are explained in the grep man page.

I know that ] are for regexp but I thought that if I put or ] between ‘’ the special meaning is eliminated. Is there a workaround for treating all characters in the searched string liberally? I mead a special flag for grep command. Or how can I do this without escaping the meta characters with sed or something else?

The " characters are used by the shell to escape e.g. white space. In that process they are “Eaten” by the shell and the resulting string is given as an argument field to the program called (grep in this case). So grep will never see these ".

When you want " character in a string given as a argument field to a program (which is not something that will help you here) you must escape the ", e.g. by putting a \ before the ".

henk@boven:~> echo "\""

The same as you want to put a \ in the argument. It must be escaped, because the shell eats it:

henk@boven:~> echo \\

See how we grep a in the file aap

henk@boven:~> cat aap
 aap  noot
henk@boven:~> grep  aap
grep: Ongeldige reguliere expressie
henk@boven:~> grep \ aap
grep: Ongeldige reguliere expressie
henk@boven:~> grep \\ aap
 aap  noot

or with ’ escape in the shell and \ in grep:

henk@boven:~> grep '\' aap
 aap  noot

Nice isn’t it? one can go and try all sorts of combinations for hours and hours …

No, the shell eats the quote characters. You need some kind of escaping or quoting at the shell, otherwise the is special to the shell. So you have two stages to think about, 1. how do I get these characters to the program the way I want, and 2. how to tell the program that is not special. Let’s try it with just the echo program.

echo \\
echo '\'
echo "\"

will all get \ to echo.

After some practice you get used to this.

My problem is that the search string is given by a script so… I don’t know how it’s look like. I need an option or another utility that doesn’t interpret regexp characters.

If you just want to use grep as a simple matcher, then look at the --fixed-strings option. This turns off regexp interpretation of the characters and the only special character is the newline, which separates strings. But it also means you can search for more than one string at once.

You will still have to get any shell metacharacters past the shell. If you single quote the whole string, then the only special character is the single quote itself which you will have to escape as this sequence: ‘"’"’. Explaination: end the single-quoted string, start a double-quoted string, real single-quote, end the double-quoted string, and start a new single-quoted string. So you only need to escape the single quote this way before giving it to grep --fixed-strings.

If you are calling grep from a program (e.g. C, Perl, Python, etc) it is possible to avoid calling the shell, and then you don’t have to worry about escaping single-quotes.

Thanks! the -F flag is just what I was looking for.