I have script which will take a string as input and search in a file.
But when I want to search a pattern which has special characters script is ignoring it.
For example: I want to search a pattern "\.tumblr\.com". shell script is removing \ (backslah) and trying to search ".tumblr.com" in a file. File has pattern \.tumblr\.com. How to read a pattern as input as it is(without removing backslah) using shell script.
Script I am using is bourne shell script.
Below is bash version of my system.
/usr/bin/bash --version
GNU bash, version 3.00.16(1)-release (sparc-sun-solaris2.10)
Please help me to find the solution. I appreciate any ones help.
For example I have two different patterns
"groups\.google\.com/group\/"
"groups\.google\.com\/group\/"
If I want to search above patterns I need to escape special characters. I can escape it by backslah in sed. Like below commands. I tried them on command line not in script.
sed -n '/groups\\.google\\.com\/group\\\//p' file ( matches "groups\.google\.com/group\/")
sed -n '/groups\\\.google\\\.com\\\/group\\\//p' file ( matches "groups\.google\.com\/group\/" )
But the problem is I can only use one command at once to search any of the patterns. I want both the patterns to be matched by one command.
I am appending below code I am using... Its bit cryptic. I am using -s option to substitute all the special characters with backslah.
------------------------------
echo "Enter the heuristic value"
read heu
reg=`echo "$heu" | sed 's:\.:\\\\.:g; s:[]\[\+\?\-\ \*]:\\\&:g; s:\-[^z9].:\\\\\\\\\\\&:g; s:/:\\\\\\\\\\\/:g;'`