awk/sed with special characters

i have this script that searches for a pattern.
However it fails if the pattern includes some
special characters. So far, it fails with the
following strings:
1. -Cr
2. $Mj
3. H'412
would a sed or awk be more effective?
i don't want the users to put the (\)
during the search (they are not familiar with unix)...
help is very much appreciated!

#!/bin/ksh

case $# in
0) echo "\n\t No argument(s) entered! Try again."
echo "\t Usage: whereis arg1 [arg2] [arg3]"
exit 1 ;;
) grep -n "$" file | cut -f1 -d: > lineno
cat lineno ;;
esac

if sed/awk can be used, how can i print the line numbers?

The shell does processing to command lines long before it invoke the command. A command like:
something $xyz
is just not going to fly. The shell is going to look for a variable called xyz and substitute that value. Compare the above command to:
echo $PATH

The variable names are gone before the command runs.

Maybe you could read in the string instead of entering it on the command line?

#! /usr/bin/ksh
IFS=""
print -n "enter search string -"
read string
sed -n '/'"${string}"'/=' < file
exit 0

should do it if you just want only the line number printed.

Thanks Perderabo!

i tried your code and it works great. although i had an error
when i entered the string df/DF.

apalex>cat infile
pattern1 $Major
pattern2 -Crit
pattern3 df/DF
pattern4 h'405
pattern5 \ffe
pattern6 ab >
pattern7 abc >>
pattern8 A"a"
pattern9 `0de`

apalex> cat whereis
#! /usr/bin/ksh
IFS=""
print -n "enter search string -"
read string
sed -n '/'"${string}"'/=' < infile
exit 0

apalex> whereis
enter search string -df/DF
sed: command garbled: /df/DF/=
apalex>

I just thought its easier to type the string on the
command line, so i tried to insert your code in
my script but it really gives me a lot of errors...

apalex> cat whereis1
#!/bin/ksh
case $# in
0) echo "\n\t No pattern(s) entered! Try again."
echo "\t Usage: whereis1 pattern1 [pattern2]"
exit 1 ;;
) sed -n '/'"${}"'/=' < infile
exit 0 ;;
esac

it doesn't work on patterns:
$Major, ab > and abc >>

I really appreciate this forum, gives me a chance to improve my
skills. Thanks a lot guys!!!

Perderabo, can you please explain further the line below?
i can't follow the logic of using ", ' ,= and {} on the sed.

sed -n '/'"${string}"'/=' < infile

Is there any fix in the code for searching for string with "/" on it?
Please see below. Thank you.

Well ${string} is a variable reference like $string but more secure for adjacent characters. I could probable get away with just $string in this case.

I put the variable refernce in double quotes to protect any white space in the value. Everything else in the sed command is in single quotes to prevent anything at all from happening to them.

But the quotes and braces are shell things not sed things. sed will never see them.

As for dealing with the slash, it is possible but would require so much code that I would drop ksh and switch to c. When a script becomes more complex than the c program would be, it's time to switch languages.

Thanks for the info!