Bash show charcaters if not in string

前端 未结 2 633
情话喂你
情话喂你 2021-01-15 22:08

I am trying out bash, and I am trying to make a simple hangman game now.

Everything is working but I don\'t understand how to do one thing:

I am showing the

相关标签:
2条回答
  • 2021-01-15 22:43

    You don't show what you tried, but parameter expansion works fine.

    $ alphabet=abcdefghijklmnopqrstuvwxyz
    $ word="hello world"
    $ guesses=aetl
    $ echo "${word//[^[:space:]$guesses]/*}"
    *ell* ***l*
    $ echo "${alphabet//[$guesses]/*}"
    *bcd*fghijk*mnopqrs*uvwxyz
    
    0 讨论(0)
  • 2021-01-15 22:44

    First store both strings in files where they are stored one char per line:

    sed 's/./&\n/g' | sort <<< $guess > guessfile
    sed 's/./&\n/g' | sort <<< $word > wordfile
    

    Then we can filter the words that are only present in one of the files and paste the lines together as a string:

    grep -xvf guessfile wordfile | paste -s -d'\0'
    

    And of course we clean up after ourselves:

    rm wordfile
    rm guessfile
    

    If the output is not correct, try switching arguments in grep (i.e. wordfile guessfile instead of guessfile wordfile).

    0 讨论(0)
提交回复
热议问题