943,685 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Nov 23rd, 2008
0

Beginner in UNIX/Shell Script - Please help

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
learnpro is offline Offline
6 posts
since Nov 2008
Nov 23rd, 2008
0

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
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007
Nov 24th, 2008
0

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
learnpro is offline Offline
6 posts
since Nov 2008
Nov 25th, 2008
0

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:

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
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007
Nov 25th, 2008
0

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
learnpro is offline Offline
6 posts
since Nov 2008
Nov 26th, 2008
0

Re: Beginner in UNIX/Shell Script - Please help

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
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007
Nov 28th, 2008
0

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.
Reputation Points: 13
Solved Threads: 9
Junior Poster in Training
omrsafetyo is offline Offline
58 posts
since Apr 2008
Nov 28th, 2008
0

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
learnpro is offline Offline
6 posts
since Nov 2008
Nov 28th, 2008
0

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
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007
Nov 29th, 2008
0

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
learnpro is offline Offline
6 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: difference btw a process and a job
Next Thread in Shell Scripting Forum Timeline: IPV6





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC