Hello Everyone,
I am a total beginner in UNIX/Shell script. I am in the process of creating a huge script for a spellchecker program. What it does is spellcheck files and throws output in a temporary file. What I need help in is forming a for loop where it will output each misspelled word, one at a time asking user to correct it or leave it misspelled. Now the corrections go to another temporary file and the words that are not corrected goes to another file. I need to use grep to find and sed to replace each occurence of the same word. I am stuck at this part for over 2 weeks ....Please please help if anyone can. I will really appreciate all or any suggestions. I will post what i have ...

filename1=file where output of spell is in
filename2= file where corrections are going
filename3=words not corrected are directed

for $word in `cat filename1`; do
echo $word
grep -n $word $filename1 | echo "incorrect $word"
echo "do you want to fix the incorrect word"
echo -n "c)orrect, m)ove"
read in
if [ "$in" = "c" -o "$in" = "correct" ] ; then
vi $filename1
sed "s/$word/$in/g" >> $filename2
elif [ "$in" = "m" -o "in" ="move" ] ; then
$word >> $filename3
fi

Thanks very much.

Hey there,

This question has been answered on this board a few times, but the first thing you should look at is how you're calling your variables and iterating through them. For instance:

for $word in `cat filename1`; do
echo $word

should be

for word in `cat filename1`; do
echo $word

with "word" being the variable and $word being the content of that variable.

Once you get past that, I'm slightly confused about filename1 - does it contain only incorrectly spelled words? If so, it might not be necessary to try to grep the words that you're pulling from it against it. They'll be there because you just read them from the file.

I may be missing something.... ?

Best wishes,

Mike

Thank you very much. I sure could use the way you suggested to start the for loop.
filename1 is for file that will store spelling errors that spellchecker just checked. Now, existence of these files is mandatory per program requirements, so I have to use it though it can be simple without it.
Any further suggestions as to how to make the loop working and correct would be appreciated.
Again thank you all for your time and expertise.

Hey Again,

This may or may not be a huge improvement, but here's a quick rewrite of your original script so that it does what it thinks its supposed to. If this ends up not being what you want, then you can tell me where I screwed up and it'll be easier to fix ;)

Just kidding, of course :)

Best wishes, and I look forward to your reply (from what I just rewrote, I noticed two other things - one regarding your grep if conditional and the other is having done at the end of your "for do" loop:

#!/bin/bash

filename1=spell_output
filename2=corrections
filename3=not_corrected

for word in `cat filename1`
do
    echo $word
    result=`grep -n $word $filename1 >/dev/null 2>&1`
    if [ $result -eq 0 ]
    then
        echo "incorrect $word"
        echo "do you want to fix the incorrect word"
        echo -n "c)orrect, m)ove"
        read in
        if [ "$in" = "c" -o "$in" = "correct" ]
        then
            vi $filename1 
            sed "s/$word/$in/g" >> $filename2
        elif [ "$in" = "m" -o "in" ="move" ]
        then
            $word >> $filename3
        fi
    fi
done

Thanks very much for your response. I tried the code and it is giving this error that i was unable to correct:

[: -eq: unary operator expected

I believe it is for " if [ $result -eq 0 ] " line of code. Any thoughts or suggestions what can I do to correct it. Again, I appreciate all your help.

I am "really" sorry - I totally goofed that and didn't even realize :P

This is what I meant to type (wanting to get the value of the return - errno or $?

grep -n $word $filename1 >/dev/null 2>&1
result=$?
if [ $result -eq 0 ]

Just those first two lines need to change in the script.

Sorry about that, again. That'll teach me to do more than "bash -n" to check my post answer ;)

, Mike

I didn't realize you could call vi in a script like that and execute sed from there - I always sed the file and output to a temp file, and then read back in to the original... this is much cleaner.

Thank you very much again Mike. Your expertise certainly directed me towards the right direction...I am still working on manipulating the entire script though. One question I do have is, when the user enters the vi to make changes to the spellings, how do the user comes out of it. I tried using regular options :x, :w etc which does not work..program kind of hangs on at this point. Could you or someone please clarify?
Thanks.

No problem,

Depending upon your implementations behaviour, you might just have to use :wq! (with the ! - exclamation point - character being of great importance since it tries to "force" situation.

Otherwise, check out this post on how to write vi and control codes into your script. I wrote it, but it's hard one to cut-and-paste. You'll see why ;)

http://linuxshellaccount.blogspot.com/2007/11/editing-startup-scripts-on-solaris-part_11.html

Best wishes,

Mike

Thank you so much Mike, I got it now. You have been very helpful and great.

Now, let's say i want to create a script file for sed and apply the output of scriptfile to original file in a different statement. Here is what I did:

Change statement: sed "s/$word/$in/g" >> $filename2
to echo "s/$word/$in/g" >> filename2;

following the section of the program , I should add:

sed -f $filename2 $originalfile

Where original file is the file we checked for spellings, so now we need to send sed's output to it.
however, this approach is not working, the program runs fine with no errors, but when I check original file, it still has same spelling mistakes. There must be something I am missing! Any thoughts/suggestions. Please help.
Thanks very much.

Hey there,

Glad to help :)

You should be all right with that, just be sure to escape your shell special character when you echo:

echo "s/$word/$in/g" >> filename2

would end up echoing the value of your variables into your sed script.

echo "s/\$word/\$in/g" >> filename2

should take care of that :)

, Mike

Thanks again for replying. I would not have bothered you again, but the syntax seems to be missing something on my end.
sed -f "filename2" "originalfile"
Doing so does not apply the output of filename2 to the original file. Strangely, when I use sed without -n option, it displays the output correctly on stdout, however, same does not apply to the file when I takeoff -n. Any thing you may suggest to fix this !
Thanks and I appreciate all your help.

Hey,

The important thing is that you're making forward progress! :)

For the -n flag, you're instructing sed not to print anything - which is good when you don't want to print every single line of your file. So, it kind of works like a "grep" in the sense that, if you end your sed statement with the "p" flag and use -n, you will only print the lines that match.

So, for instance:

sed -n '/HELLO/p' filename

would only print the lines from "filename" that matched the regular expression HELLO.

I think if you just add the p to the end of your expression (works on straight-up match operator, like above, and at the end of a substitute, like you're doing (e.g. sed -n 's/this/that/p' FILE)

Best wishes,

Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.