| | |
Beginner in UNIX/Shell Script - Please help
![]() |
•
•
Join Date: Nov 2008
Posts: 6
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
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:
should be
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
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
•
•
•
•
for word in `cat filename1`; do
echo $word
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
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
•
•
Join Date: Nov 2008
Posts: 6
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
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:
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:
Shell Scripting Syntax (Toggle Plain Text)
#!/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
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
I am "really" sorry - I totally goofed that and didn't even realize 
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

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
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
•
•
Join Date: Nov 2008
Posts: 6
Reputation:
Solved Threads: 0
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.
Thanks.
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
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
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
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
•
•
Join Date: Nov 2008
Posts: 6
Reputation:
Solved Threads: 0
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.
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.
![]() |
Other Threads in the Shell Scripting Forum
- Previous Thread: difference btw a process and a job
- Next Thread: IPV6
| Thread Tools | Search this Thread |





