How to process special characters with grep - Bourne Shell.

I have a text, like so:

$ cat jim.txt
There was a boy whose name was Jim;
His friends were very good the him.
They gave him tea and cakes and jam
And slices of delicious ham
And chocolate with pink inside
And little tricycles to ride
And read him stories through and through
And even took him to the zoo -
But there it was the dreadful fate
Befell him, which I now relate.

Then, I have a script that does concordance analysis. It takes each word, finds where its located (line#,position#) and outputs, alphabetically, like so:
aaa 1,3 1,7
bbb 1,2 2,6
ccc 3,5, 4,22 .. and like so. (For a text that had multiple aaa, bbb, ccc)

The script goes like this:

#!/bin/sh

n=0 # counter for line numbers
tmp_file=/tmp/tmp.$$.txt
words_file=/tmp/tmp.words_list.$$.txt

touch $tmp_file

# analyze input text
while read line
do
n=`expr $n + 1`
echo $line | awk '{ for (i = 1 ; i <= NF; i++ ) { print tolower($i) " " line_num "," i } }' line_num=$n
>> $tmp_file
done

cat  $tmp_file | awk '{ print $1 }' | sort | uniq > $words_file

#echo "output --->"
for w in `cat $words_file | sort`
do
        echo -n "$w "
        grep "$w " $tmp_file | awk '{ printf " %s", $2 }'
        echo " "
done

rm -f $tmp_file
rm -f $words_file

exit 0

This causes a problem with grep, giving an error (If I run the script) like:
- grep: illegal option --  
Usage: grep -hblcnsviw pattern file . . .

everytime it hits a '-'. It tries to grep the '-' from the line below.
aaa 1,2 4,7
- 6,22 3,76

Before the while read line, I tried putting a sed 's/-/=/g' , so that I can replace it after with '-' after the grep, but it doesnt work since by that time Its already output.

Any ideas? Either avoid using grep, or re-substitute the = with - at some point after the grep.
vupadhyaAsked:
Who is Participating?
 
brettmjohnsonConnect With a Mentor Commented:
when grep sees the '-' character in the argument, it assumes
that argument is a switch.  You want to precede your pattern
with a '-e' switch so that it is explicitly interpreted as a pattern:

      grep -e "$w " $tmp_file | awk '{ printf " %s", $2 }'

from the man page:

       -e PATTERN, --regexp=PATTERN
              Use PATTERN as the pattern; useful to protect patterns beginning
              with -.


0
 
vupadhyaAuthor Commented:
In the bourne shell (sh), the -e is not a valid option.
0
 
ahoffmannCommented:
the -e option is for grep, not the shell
if your grep has no -e then you better install GNU's grep
0
 
vupadhyaAuthor Commented:
I needed it to use:

 /usr/xpg4/bin/grep -e "$w " $tmp_file | awk '{ printf " %s", $2 }'

Thanks!

0
All Courses

From novice to tech pro — start learning today.