How to process special characters with grep - Bourne Shell.

Posted on 2005-04-23
Last Modified: 2013-12-26
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:


n=0 # counter for line numbers

touch $tmp_file

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

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

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

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.
Question by:vupadhya
    LVL 23

    Accepted Solution

    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 -.


    Author Comment

    In the bourne shell (sh), the -e is not a valid option.
    LVL 51

    Expert Comment

    the -e option is for grep, not the shell
    if your grep has no -e then you better install GNU's grep

    Author Comment

    I needed it to use:

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



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
    Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now