Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easy way to pass a "raw" string to grep?

grep can't be fed "raw" strings when used from the command-line, since some characters need to be escaped to not be treated as literals. For example:

$ grep '(hello|bye)' # WON'T MATCH 'hello' $ grep '\(hello\|bye\)' # GOOD, BUT QUICKLY BECOMES UNREADABLE 

I was using printf to auto-escape strings:

$ printf '%q' '(some|group)\n' \(some\|group\)\\n 

This produces a bash-escaped version of the string, and using backticks, this can easily be passed to a grep call:

$ grep `printf '%q' '(a|b|c)'` 

However, it's clearly not meant for this: some characters in the output are not escaped, and some are unnecessarily so. For example:

$ printf '%q' '(^#)' \(\^#\) 

The ^ character should not be escaped when passed to grep.

Is there a cli tool that takes a raw string and returns a bash-escaped version of the string that can be directly used as pattern with grep? How can I achieve this in pure bash, if not?

like image 998
slezica Avatar asked Aug 08 '12 00:08

slezica


People also ask

Can you grep RegEx?

grep is one of the most useful and powerful commands in Linux for text processing. grep searches one or more input files for lines that match a regular expression and writes each matching line to standard output.

Why are raw strings used in RegEx?

Raw strings help you get the "source code" of a RegEx safely to the RegEx parser, which will then assign meaning to character sequences like \d , \w , \n , etc...

How do you define a raw string?

Python raw string is created by prefixing a string literal with 'r' or 'R'. Python raw string treats backslash (\) as a literal character. This is useful when we want to have a string that contains backslash and don't want it to be treated as an escape character.

What is raw string in python RegEx?

According to Python docs, raw string notation (r"text") keeps regular expressions meaningful and confusion-free. Without it, every backslash ('\') in a regular expression would have to be prefixed with another one to escape it. For example, the two following lines of code are functionally identical − >>> re.


1 Answers

If you want to search for an exact string,

grep -F '(some|group)\n' ... 

-F tells grep to treat the pattern as is, with no interpretation as a regex.

(This is often available as fgrep as well.)

like image 139
ephemient Avatar answered Sep 19 '22 18:09

ephemient