DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Shell Scripting (http://www.daniweb.com/forums/forum113.html)
-   -   Beginner in UNIX/Shell Script - Please help (http://www.daniweb.com/forums/thread159096.html)

learnpro Nov 23rd, 2008 2:41 pm
Beginner in UNIX/Shell Script - Please help
 
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.

eggi Nov 23rd, 2008 11:50 pm
Re: Beginner in UNIX/Shell Script - Please help
 
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:

Quote:

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

Quote:

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

learnpro Nov 24th, 2008 12:37 am
Re: Beginner in UNIX/Shell Script - Please help
 
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.

eggi Nov 25th, 2008 1:15 am
Re: Beginner in UNIX/Shell Script - Please help
 
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

learnpro Nov 25th, 2008 11:51 pm
Re: Beginner in UNIX/Shell Script - Please help
 
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.

eggi Nov 26th, 2008 2:16 am
Re: Beginner in UNIX/Shell Script - Please 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

omrsafetyo Nov 28th, 2008 8:07 pm
Re: Beginner in UNIX/Shell Script - Please help
 
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.

learnpro Nov 28th, 2008 9:19 pm
Re: Beginner in UNIX/Shell Script - Please help
 
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.

eggi Nov 28th, 2008 11:53 pm
Re: Beginner in UNIX/Shell Script - Please help
 
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.co...s-part_11.html

Best wishes,

Mike

learnpro Nov 29th, 2008 12:59 pm
Re: Beginner in UNIX/Shell Script - Please help
 
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.


All times are GMT -4. The time now is 4:48 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC