I have a user input that would be used in a search string that may contain a metacharacter
For e.g. C# or C++
my grep command in a function was:
grep -E "$1|$2" test.txt
under direct replacement:
grep -E "C\+\+|testWord" test.txt
grep -E "C\#|testWord" test.txt
the first caught the lines fine but not the second. Strangely, # was completely ignored. Without direct replacement, both catch anything with c followed by testWord instead of c++ and c# respectively
I've tried handling it using sed
$temp = `echo $1 | sed 's/[\#\!\&\;\`\"\'\|\*\?\~\<\>\^\(\)\[\]\{\}\$\+\\]/\\&/g'`
but it doesn't work right. Or is there any other way to handle user input with metacharacters?
Thanks in advance