LinuxSA Mailing list archives

Index: [thread] [date] [subject] [author] [stats]
  From: Jake Hawkes <jake@infinitylimited.net>
  To  : Philippe Doucet <philippedoucet@hotmail.com>
  Date: Sun, 01 Apr 2001 17:30:11 +0000

Re: Bash script question.

Philippe Doucet wrote:
> 
> Philippe Doucet wrote:
> 
> >
> > I have redhat 6.2 running bash
> > I`m trying to writte a hangman script and I`m stuck with this
> problem.
> >
> > I have a file containing spaced out underscore ie. - - - - - - -
> >
> > The number of underscores represent the number of letter of the word
> > to be discovered.
> >
> > Once a user makes a guess at the word, let`s say the letter A, and
> the
> > user is right, I want that letter to replace the appropriate
> > underscore. so let`s say that the letter A was the second letter of
> > the word, after the user guesses the letter A, the file would be
> > appended to  - A - - - - -
> need ... more ... info ....
> how are you storing the answer word?
> do you know that the 'a' is the second letter? What if there are more
> than 1 'a' iin the answer? How do you store that info?
> here's a tip, shell programming regular expressions sucks I reckon.
> I say regexp because that's how I'd do it.
> make a regexp that replaces the 2nd instance of "- " pattern with "A"
> --
> Jake Hawkes B.Eng, (CSE)
> Paul's Law: You can't fall off the floor.
> 
> Hmmm ok...well here it is...yes if there is more then one A i want it
> to replace it as well...so in my command i'll be using some $1 $2
> $3....for sure the word is stored in a file, that file is translated
> into all underscore and stored in another file. Now I prompt the user
> to enter a letter, I check if that letter exist in the word and also
> where it is situated in the word, that is all fine. Now i'm at where I
> take that letter and replace it in the right order into the underscore
> file. I was thinking of regular expression but all i can find is
> commands that will append line by line...since my whole word is on the
> same line i don't know how to act on each individual underscore. I"m
> sorry for the way i'm giving out information but i'm quite new to
> bash...like 4 weeks new ...i'm pretty sure a regexp would be the
> answer.
 

Firstly some mailing tips:
	- try paragraphs, they're great
	- keep the same subject line, that way all the messages will be related
and threads work
	- loose the HTML :)

The only way I know to do character based replacements for an entire
file, is to do the replacement to a temporary file, then replace the
origional file with it.

so:

sed s/// orig_file > tmp_file
mv tmp_file orig_file


that I think is the only way without starting to get complicated.

when you get the position of the A, I assume you get it as a number. so,
the sed line would be something like this:

< _ represents a space >

sed s/(-\_){$i}(-\_)(-\_)*/($1)A\ ($3)/  orig_file > tmp_file

where $i is the index of the A minus 1. so if A was the second letter,
$1 = 2-1=1

so sed will match index-1 instances of -\_ (escape the space) then one
instance of -\_ and then zero or more instance of -\_  

then it will replace the second expression with A and leave the first
and second expressions the same. (When you put () around and expression,
you are saving it for later. Unfortunately, I am not 100% on the syntax
for this in bash. I would suggest using perl, because your going to want
to use it eventually anyway:))


--
Jake Hawkes B.Eng, (CSE)

Some days it's not worth chewing through the leather straps.

-- 
LinuxSA WWW: http://www.linuxsa.org.au/  IRC: #linuxsa on irc.linux.org.au
To unsubscribe from the LinuxSA list:
  mail linuxsa-request@linuxsa.org.au with "unsubscribe" as the subject


Index: [thread] [date] [subject] [author] [stats]
Return to the LinuxSA Mailing List Information Page