How to avoid a previously unknown string in a regular expression?

I need to egrep string that is unknown before execution, and what I will get through the shell variable (shell bash, if that matters). The problem is that the string will contain special characters such as braces, spaces, periods, slashes, etc.

If I know a string, I can avoid special characters one at a time, but how can I do this for the entire string?

Running a line using a sed script to prefix each special character with \ might be an idea, I still need rtfm how such a script should be written. I do not know if there are other, better options.

I read re_format(7) , but it seems that there is no such thing as "take the whole next line as a literal" ...

EDIT: to avoid false positives, I should also add newline detection to the template, for example. egrep '^myunknownstring'

+7
source share
3 answers

If you need to embed a string in a larger expression, sed is how I did it.

 s_esc="$(echo "$s" | sed 's/[^-A-Za-z0-9_]/\\&/g')" # backslash special characters inv_ent="$(egrep "^item [0-9]+ desc $s_esc loc .+$" inventory_list)" 
+7
source

Use the -F flag to make PATTERN a literal string

 $ var="(.*+[az]){3}" $ echo 'foo bar (.*+[az]){3} baz' | grep -F "$var" -o (.*+[az]){3} 
+4
source

Are you trying to protect the string from misinterpreting bash syntax, or are you trying to protect parts of the string from being interpreted as regular expression syntax?

To protect bash:

grep supports the -f switch:

 -f FILE, --file=FILE Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore matches nothing. 

No escaping is required inside the file. Just create a file containing one line (and therefore one template), which can be obtained from your shell variable if necessary.

 # example trivial regex var='^r[^{]*$' pattern=/tmp/pattern.$$ rm -f "$pattern" echo "$var" > "$pattern" egrep -f "$pattern" /etc/password rm -f "$pattern" 

Just to illustrate this point.

Try using -f instead, as another poster offers protection for regular expressions.

0
source

All Articles