$ cat -n file1 |grep -e 'def|zzz' #No results Making some similar tests with single quotes we have a different and correct behavior: $ cat file1 As a consequence, the only difference between grep and grep -E for GNU grep is what has to be escaped functionality is identical.Īccording to grep man pages, and especially according to info pages, all examples given for grep include single quotes and not double quotes. In general, it's prudent to single quote your regular expression so the shell leaves it alone.Īs a side note, I don't think your C program is representative of how the shell processes arguments in Shell Operation, quoting is a separate step and includes backslash processing (see Escape Character).ġAs an extension, GNU grep allows you to escape | in BRE and get alternation. grep -e"def\\\|zzz" – the shell turns this into def\\|zzz ( \\ becomes \, \| isn't special to the shell and stays unchanged) grep sees \\ as a literal backslash ( backslash escaped by backslash), so | isn't special, and grep tries to match the exact string def\|zzz.grep -e"def\\|zzz" – \\ is special according to the manual excerpt (try echo "\\") grep sees def\|zzz because the shell removes a backslash, and the behaviour is the same as for the second case.grep -e"def\|zzz" – | isn't one of the special characters mentioned above, so grep receives def\|zzz, and GNU grep treats \| as alternation 1.grep -e"def|zzz" – grep receives def|zzz because it defaults to basic regular expressions (BRE), | isn't special 1 and grep tries to match the literal string def|zzz.This means that your expressions are treated as follows: Within double quotes, backslashes that are followed by one of these characters are removed. The backslash retains its special meaning only when followed by one of the following characters: $, `, ", \, or newline. If you double quote your regex, the shell treats backslashes specially (emphasis mine): I suspect grep itself does something special with the literal string \\|. So all double-quotes are removed and the backslashes and pipes are not altered by the shell. I investigated what my shell does with the command lines above : Using this simple argument-printing program, void main(int argc, char** argv) Escaping with three backslashes fails, echo abcdef | grep -e"def\\\|zzz"ĭoes anyone have an explanation, especially for the 2-backslash case ? More surprisingly, escaping with 2 backslashes also works, echo abcdef | grep -e"def\\|zzz" Escaping with one backslash works, echo abcdef | grep -e"def\|zzz" Outputs nothing, because grep is not in extended regex mode. Which certainly does support non-capturing groups.I'm a little confused about how many backslashes are needed to escape the alternation operator | in regular expressions for grep. Extended regular expressions will handle these atoms and operators properly, so use egrep, grep -E, or pcregrep depending on what's available on your particular system. Standard grep uses a regular expression engine that doesn't understand ranges, and that requires special characters to be escaped. Use Extended Regular Expressions with Grep
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |