Beginner in UNIX/Shell Script - Please help

Reply

Join Date: Nov 2008
Posts: 6
Reputation: learnpro is an unknown quantity at this point 
Solved Threads: 0
learnpro learnpro is offline Offline
Newbie Poster

Beginner in UNIX/Shell Script - Please help

 
0
  #1
Nov 23rd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Beginner in UNIX/Shell Script - Please help

 
0
  #2
Nov 23rd, 2008
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
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!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 6
Reputation: learnpro is an unknown quantity at this point 
Solved Threads: 0
learnpro learnpro is offline Offline
Newbie Poster

Re: Beginner in UNIX/Shell Script - Please help

 
0
  #3
Nov 24th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Beginner in UNIX/Shell Script - Please help

 
0
  #4
Nov 25th, 2008
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:

Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/bash
  2.  
  3. filename1=spell_output
  4. filename2=corrections
  5. filename3=not_corrected
  6.  
  7. for word in `cat filename1`
  8. do
  9. echo $word
  10. result=`grep -n $word $filename1 >/dev/null 2>&1`
  11. if [ $result -eq 0 ]
  12. then
  13. echo "incorrect $word"
  14. echo "do you want to fix the incorrect word"
  15. echo -n "c)orrect, m)ove"
  16. read in
  17. if [ "$in" = "c" -o "$in" = "correct" ]
  18. then
  19. vi $filename1
  20. sed "s/$word/$in/g" >> $filename2
  21. elif [ "$in" = "m" -o "in" ="move" ]
  22. then
  23. $word >> $filename3
  24. fi
  25. fi
  26. 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!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 6
Reputation: learnpro is an unknown quantity at this point 
Solved Threads: 0
learnpro learnpro is offline Offline
Newbie Poster

Re: Beginner in UNIX/Shell Script - Please help

 
0
  #5
Nov 25th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Beginner in UNIX/Shell Script - Please help

 
0
  #6
Nov 26th, 2008
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
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!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 58
Reputation: omrsafetyo is an unknown quantity at this point 
Solved Threads: 9
omrsafetyo omrsafetyo is offline Offline
Junior Poster in Training

Re: Beginner in UNIX/Shell Script - Please help

 
0
  #7
Nov 28th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 6
Reputation: learnpro is an unknown quantity at this point 
Solved Threads: 0
learnpro learnpro is offline Offline
Newbie Poster

Re: Beginner in UNIX/Shell Script - Please help

 
0
  #8
Nov 28th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Beginner in UNIX/Shell Script - Please help

 
0
  #9
Nov 28th, 2008
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
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!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 6
Reputation: learnpro is an unknown quantity at this point 
Solved Threads: 0
learnpro learnpro is offline Offline
Newbie Poster

Re: Beginner in UNIX/Shell Script - Please help

 
0
  #10
Nov 29th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC