Solved

Unix pattern macthin help needed

Posted on 2007-11-16
19
283 Views
Last Modified: 2011-09-20
Hi guys i'm tryin to write a hangman script and need a bit of help wit some writing a function.
I'm tryin to wite a hangman game andhave got a bit stuck now. I'm tryin to compare a inputed letter into the script with the secret word of the game.  without the use of sed or awk. e.g if a user inputs "a"i want t compare the letter to every letter in the secret word of the game i dont want to use sed or awk so an example with erm will not gain the points thankyou or ur help
0
Comment
Question by:DSwiftie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
  • 2
19 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 20303045
if echo $inputed_letter |  grep [$secretword] ; then echo match ; fi
0
 

Author Comment

by:DSwiftie
ID: 20303684
lets say at the start of the game i wanted to use "_" to represent each letter how do i get and every time the correct letter is inputed how do i get it to first display the _ and replace each underscore evertime the correct letter is inputed
0
 
LVL 84

Expert Comment

by:ozo
ID: 20303709
echo $secretword | tr -c $inputed_letter _
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 20303718
You may want to either make that echo -n or initialize $inputed_letters to \n
0
 

Author Comment

by:DSwiftie
ID: 20305047
wat do u mean by intialize it to \n not ever used it before
0
 

Author Comment

by:DSwiftie
ID: 20305212
oh yea one more thing how do i get the script to show a  _ for every letter of the word when the game is started i know its a simple question but it makes it easier to know how to replace each _

cheers for ur help me well appricated
0
 

Author Comment

by:DSwiftie
ID: 20305218
think to better understand the code to replace the _ i need to understand how to get each letter to print a _ for letter of secreword
0
 
LVL 84

Expert Comment

by:ozo
ID: 20306936
echo -n $secretword | tr -c _ _
0
 

Author Comment

by:DSwiftie
ID: 20307365
that commands really helps but i'm tryin to make sure sure the speace is left between each _

get if therunning word is dog

then it should print _ _ _ and not ___ as that above one sems to be doin sori to b a pain i'm a complete noob at unix
0
 
LVL 84

Expert Comment

by:ozo
ID: 20307533
You said no sed or awk, but what about perl?
0
 

Author Comment

by:DSwiftie
ID: 20308141
have nevver even heard of perl i might have to do it using a while statement of somethin i gues while cutin each letter of the word out to print each of the _ _ prob is replacing the _ with a letter will be my main problem. And here i thought i was finished with teh script. Nywayz m8 thankyou very much for your help i know i ended up askin more than i had stated above thx for the help
0
 

Author Comment

by:DSwiftie
ID: 20309408
just out of inrest how wuld i do the _ _ _ using perl b4 i start tryin ny long complicated while loops
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20309839
Why the restriction with sed or awk?  Using Perl would be in the same category as using sed/awk.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20309854
For example, to use Perl, you'd do

echo dog | perl -pe 's/./_ /g'

which the sed equivalent is

echo dog | sed "s/./_ /g"
0
 

Author Comment

by:DSwiftie
ID: 20316267
i can't explain sed and awk n i just realised perl thus so i dont want to use erm i wuld have prefered not usin some special lang completely tbh
0
 
LVL 84

Expert Comment

by:ozo
ID: 20316872
shells are also special languages,  and not languages particularly suited to the task

#!/bin/bash
for (( i=${#secretword}; $i ; --i )) ; do echo -n "_ " ; done
0
 
LVL 84

Expert Comment

by:ozo
ID: 20316916
#!/bin/bash
echo ${secretword//?/_ }
0
 
LVL 84

Accepted Solution

by:
ozo earned 350 total points
ID: 20316961
#!/bin/bash
secretword="d o g"
guesses=" _"
while [[ "${secretword//[^$guesses]/}" != "$secretword" ]] ; do
  read guess
  if [[ ${guess/[a-z]/_} == "_" && ${guess/[^$guesses]/#} == "#" ]] ; then
    guesses="$guesses$guess"
    echo ${secretword//[^$guesses]/_}
  fi
done
0
 

Author Comment

by:DSwiftie
ID: 20318726
that will do thankyou vey much for your help m8 well appricated culd never hav done with without u
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question