problem with Regular expression as input in shell script

Hi,

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.

Try:

grep '\\\.tumblr\\\.com' infile

Thanks for the reply..

But I cannot grep the pattern directly because I want to take pattern as input see below.

echo "Enter the pattern:"
read 'value'
echo "Checking for the pattern..."
echo "---------------------------"
fgrep "$value" file
echo "---------------------------"

so when I give "\.tumblr\.com" as input, shell is removing backslah(\). I dont want backslah to be removed..

Please help...

Hi, you can use:

read -r value

Thanks for your help...it works....

But I have another doubt...

how to use regular expression patterns using sed.

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;'`

------------------------------

Please help...

You can make your life a lot easier by not using the deprecated backticks and by using $( ... ) instead. E.g.

$ heu='?a*'
$ reg=$(echo "$heu" | sed 's/[?+*]/\\&/g')
$ echo $reg
\?a\*

You are the best!!!....

I got the output what I want...

However, could you please explain the below queries I have

what is read -r
what is the difference between backquote(`) and $(...) and what is the purpose of $(...) (I guess shell will treat it as variable)

I truly appreciate your help..

cheers!!

---------- Post updated 12-22-09 at 11:42 AM ---------- Previous update was 12-21-09 at 06:49 PM ----------

hi, could you please tell about my above queries..

---------- Post updated at 12:48 PM ---------- Previous update was at 11:42 AM ----------

Could you please answer my above queries...

read options

Syntax Substitution