Leftover characters after comparing two strings

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












I'm currently writing a game in bash that will compare user input against computer input.



I want to find the leftover characters after a comparison between two strings. Below is what I have in mind:



user_word='hello' 

computer_word='bxolq'

compare $user_word $computer_word

compare function: (finds "l" to be equal in the two strings)

calculate leftover word for user (= "heo")

calculate leftover word for computer (= "bxoq")


Now the computer wins because "bxoq" length is 4 and user leftover "heo" is 3.



I tried diff to solve the problem, but the output of



diff <(echo $user_word | sed 's:(.):1n:g' | sort) <(echo $computer_word | sed 's:(.):1n:g' | sort)


puzzles me.



So my question is: how can I accomplish the leftover comparison?










share|improve this question



















  • 1




    puzzles you how? what did you expect, and what did you get? You may find comm more useful here e.g. to get an ordered list of common characters comm -12 <(fold -w1 <<< "$computer_word" | sort) <(fold -w1 <<< "$user_word" | sort)
    – steeldriver
    Dec 31 '16 at 15:08










  • @steeldriver: with the given example I think we want sort -u
    – dave_thompson_085
    Jan 1 '17 at 6:53










  • I finally managed to get my script working, thanks to steeldriver. If you are interested you can find it on github: github.com/kzpm/wordwar/blob/master/wordwar
    – kzpm
    Jan 5 '17 at 23:25










  • I notice that o is also in common; was it not removed because it's in a different position?
    – Jeff Schaller
    Apr 13 at 16:03














up vote
3
down vote

favorite












I'm currently writing a game in bash that will compare user input against computer input.



I want to find the leftover characters after a comparison between two strings. Below is what I have in mind:



user_word='hello' 

computer_word='bxolq'

compare $user_word $computer_word

compare function: (finds "l" to be equal in the two strings)

calculate leftover word for user (= "heo")

calculate leftover word for computer (= "bxoq")


Now the computer wins because "bxoq" length is 4 and user leftover "heo" is 3.



I tried diff to solve the problem, but the output of



diff <(echo $user_word | sed 's:(.):1n:g' | sort) <(echo $computer_word | sed 's:(.):1n:g' | sort)


puzzles me.



So my question is: how can I accomplish the leftover comparison?










share|improve this question



















  • 1




    puzzles you how? what did you expect, and what did you get? You may find comm more useful here e.g. to get an ordered list of common characters comm -12 <(fold -w1 <<< "$computer_word" | sort) <(fold -w1 <<< "$user_word" | sort)
    – steeldriver
    Dec 31 '16 at 15:08










  • @steeldriver: with the given example I think we want sort -u
    – dave_thompson_085
    Jan 1 '17 at 6:53










  • I finally managed to get my script working, thanks to steeldriver. If you are interested you can find it on github: github.com/kzpm/wordwar/blob/master/wordwar
    – kzpm
    Jan 5 '17 at 23:25










  • I notice that o is also in common; was it not removed because it's in a different position?
    – Jeff Schaller
    Apr 13 at 16:03












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I'm currently writing a game in bash that will compare user input against computer input.



I want to find the leftover characters after a comparison between two strings. Below is what I have in mind:



user_word='hello' 

computer_word='bxolq'

compare $user_word $computer_word

compare function: (finds "l" to be equal in the two strings)

calculate leftover word for user (= "heo")

calculate leftover word for computer (= "bxoq")


Now the computer wins because "bxoq" length is 4 and user leftover "heo" is 3.



I tried diff to solve the problem, but the output of



diff <(echo $user_word | sed 's:(.):1n:g' | sort) <(echo $computer_word | sed 's:(.):1n:g' | sort)


puzzles me.



So my question is: how can I accomplish the leftover comparison?










share|improve this question















I'm currently writing a game in bash that will compare user input against computer input.



I want to find the leftover characters after a comparison between two strings. Below is what I have in mind:



user_word='hello' 

computer_word='bxolq'

compare $user_word $computer_word

compare function: (finds "l" to be equal in the two strings)

calculate leftover word for user (= "heo")

calculate leftover word for computer (= "bxoq")


Now the computer wins because "bxoq" length is 4 and user leftover "heo" is 3.



I tried diff to solve the problem, but the output of



diff <(echo $user_word | sed 's:(.):1n:g' | sort) <(echo $computer_word | sed 's:(.):1n:g' | sort)


puzzles me.



So my question is: how can I accomplish the leftover comparison?







text-processing string






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '16 at 23:03









SouravGhosh

321210




321210










asked Dec 31 '16 at 13:54









kzpm

232




232







  • 1




    puzzles you how? what did you expect, and what did you get? You may find comm more useful here e.g. to get an ordered list of common characters comm -12 <(fold -w1 <<< "$computer_word" | sort) <(fold -w1 <<< "$user_word" | sort)
    – steeldriver
    Dec 31 '16 at 15:08










  • @steeldriver: with the given example I think we want sort -u
    – dave_thompson_085
    Jan 1 '17 at 6:53










  • I finally managed to get my script working, thanks to steeldriver. If you are interested you can find it on github: github.com/kzpm/wordwar/blob/master/wordwar
    – kzpm
    Jan 5 '17 at 23:25










  • I notice that o is also in common; was it not removed because it's in a different position?
    – Jeff Schaller
    Apr 13 at 16:03












  • 1




    puzzles you how? what did you expect, and what did you get? You may find comm more useful here e.g. to get an ordered list of common characters comm -12 <(fold -w1 <<< "$computer_word" | sort) <(fold -w1 <<< "$user_word" | sort)
    – steeldriver
    Dec 31 '16 at 15:08










  • @steeldriver: with the given example I think we want sort -u
    – dave_thompson_085
    Jan 1 '17 at 6:53










  • I finally managed to get my script working, thanks to steeldriver. If you are interested you can find it on github: github.com/kzpm/wordwar/blob/master/wordwar
    – kzpm
    Jan 5 '17 at 23:25










  • I notice that o is also in common; was it not removed because it's in a different position?
    – Jeff Schaller
    Apr 13 at 16:03







1




1




puzzles you how? what did you expect, and what did you get? You may find comm more useful here e.g. to get an ordered list of common characters comm -12 <(fold -w1 <<< "$computer_word" | sort) <(fold -w1 <<< "$user_word" | sort)
– steeldriver
Dec 31 '16 at 15:08




puzzles you how? what did you expect, and what did you get? You may find comm more useful here e.g. to get an ordered list of common characters comm -12 <(fold -w1 <<< "$computer_word" | sort) <(fold -w1 <<< "$user_word" | sort)
– steeldriver
Dec 31 '16 at 15:08












@steeldriver: with the given example I think we want sort -u
– dave_thompson_085
Jan 1 '17 at 6:53




@steeldriver: with the given example I think we want sort -u
– dave_thompson_085
Jan 1 '17 at 6:53












I finally managed to get my script working, thanks to steeldriver. If you are interested you can find it on github: github.com/kzpm/wordwar/blob/master/wordwar
– kzpm
Jan 5 '17 at 23:25




I finally managed to get my script working, thanks to steeldriver. If you are interested you can find it on github: github.com/kzpm/wordwar/blob/master/wordwar
– kzpm
Jan 5 '17 at 23:25












I notice that o is also in common; was it not removed because it's in a different position?
– Jeff Schaller
Apr 13 at 16:03




I notice that o is also in common; was it not removed because it's in a different position?
– Jeff Schaller
Apr 13 at 16:03










1 Answer
1






active

oldest

votes

















up vote
0
down vote













Assuming you want to eliminate all characters that are the same in the two strings, you can do so in two calls to sed:



#!/bin/sh

player1_word='hello'
player2_word='bxolq'

player1_short=$( printf '%sn' "$player1_word" | sed "s/[$player2_word]//g" )
player2_short=$( printf '%sn' "$player2_word" | sed "s/[$player1_word]//g" )

if [ "$#player1_short" -gt "$#player2_short" ]; then
printf 'Player1 wins with "%s" ("%s") over "%s" ("%s")n'
"$player1_word" "$player1_short"
"$player2_word" "$player2_short"
elif [ "$#player2_short" -gt "$#player1_short" ]; then
printf 'Player2 wins with "%s" ("%s") over "%s" ("%s")n'
"$player2_word" "$player2_short"
"$player1_word" "$player1_short"
else
printf 'It is a draw between "%s" ("%s") and "%s" ("%s")n'
"$player1_word" "$player1_short"
"$player2_word" "$player2_short"
fi


The main operational part of this script is



player1_short=$( printf '%sn' "$player1_word" | sed "s/[$player2_word]//g" )
player2_short=$( printf '%sn' "$player2_word" | sed "s/[$player1_word]//g" )


which sets the two _short variables to the value of each _word variable with the letters from the other _word variable removed. This is done by using one of the the words in a bracketed expression ([...]) and doing a global substitution over the other word that removes each character that matches any character in the [...] expression.



Testing:



$ sh ./script.sh
Player2 wins with "bxolq" ("bxq") over "hello" ("he")





share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f333972%2fleftover-characters-after-comparing-two-strings%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Assuming you want to eliminate all characters that are the same in the two strings, you can do so in two calls to sed:



    #!/bin/sh

    player1_word='hello'
    player2_word='bxolq'

    player1_short=$( printf '%sn' "$player1_word" | sed "s/[$player2_word]//g" )
    player2_short=$( printf '%sn' "$player2_word" | sed "s/[$player1_word]//g" )

    if [ "$#player1_short" -gt "$#player2_short" ]; then
    printf 'Player1 wins with "%s" ("%s") over "%s" ("%s")n'
    "$player1_word" "$player1_short"
    "$player2_word" "$player2_short"
    elif [ "$#player2_short" -gt "$#player1_short" ]; then
    printf 'Player2 wins with "%s" ("%s") over "%s" ("%s")n'
    "$player2_word" "$player2_short"
    "$player1_word" "$player1_short"
    else
    printf 'It is a draw between "%s" ("%s") and "%s" ("%s")n'
    "$player1_word" "$player1_short"
    "$player2_word" "$player2_short"
    fi


    The main operational part of this script is



    player1_short=$( printf '%sn' "$player1_word" | sed "s/[$player2_word]//g" )
    player2_short=$( printf '%sn' "$player2_word" | sed "s/[$player1_word]//g" )


    which sets the two _short variables to the value of each _word variable with the letters from the other _word variable removed. This is done by using one of the the words in a bracketed expression ([...]) and doing a global substitution over the other word that removes each character that matches any character in the [...] expression.



    Testing:



    $ sh ./script.sh
    Player2 wins with "bxolq" ("bxq") over "hello" ("he")





    share|improve this answer


























      up vote
      0
      down vote













      Assuming you want to eliminate all characters that are the same in the two strings, you can do so in two calls to sed:



      #!/bin/sh

      player1_word='hello'
      player2_word='bxolq'

      player1_short=$( printf '%sn' "$player1_word" | sed "s/[$player2_word]//g" )
      player2_short=$( printf '%sn' "$player2_word" | sed "s/[$player1_word]//g" )

      if [ "$#player1_short" -gt "$#player2_short" ]; then
      printf 'Player1 wins with "%s" ("%s") over "%s" ("%s")n'
      "$player1_word" "$player1_short"
      "$player2_word" "$player2_short"
      elif [ "$#player2_short" -gt "$#player1_short" ]; then
      printf 'Player2 wins with "%s" ("%s") over "%s" ("%s")n'
      "$player2_word" "$player2_short"
      "$player1_word" "$player1_short"
      else
      printf 'It is a draw between "%s" ("%s") and "%s" ("%s")n'
      "$player1_word" "$player1_short"
      "$player2_word" "$player2_short"
      fi


      The main operational part of this script is



      player1_short=$( printf '%sn' "$player1_word" | sed "s/[$player2_word]//g" )
      player2_short=$( printf '%sn' "$player2_word" | sed "s/[$player1_word]//g" )


      which sets the two _short variables to the value of each _word variable with the letters from the other _word variable removed. This is done by using one of the the words in a bracketed expression ([...]) and doing a global substitution over the other word that removes each character that matches any character in the [...] expression.



      Testing:



      $ sh ./script.sh
      Player2 wins with "bxolq" ("bxq") over "hello" ("he")





      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        Assuming you want to eliminate all characters that are the same in the two strings, you can do so in two calls to sed:



        #!/bin/sh

        player1_word='hello'
        player2_word='bxolq'

        player1_short=$( printf '%sn' "$player1_word" | sed "s/[$player2_word]//g" )
        player2_short=$( printf '%sn' "$player2_word" | sed "s/[$player1_word]//g" )

        if [ "$#player1_short" -gt "$#player2_short" ]; then
        printf 'Player1 wins with "%s" ("%s") over "%s" ("%s")n'
        "$player1_word" "$player1_short"
        "$player2_word" "$player2_short"
        elif [ "$#player2_short" -gt "$#player1_short" ]; then
        printf 'Player2 wins with "%s" ("%s") over "%s" ("%s")n'
        "$player2_word" "$player2_short"
        "$player1_word" "$player1_short"
        else
        printf 'It is a draw between "%s" ("%s") and "%s" ("%s")n'
        "$player1_word" "$player1_short"
        "$player2_word" "$player2_short"
        fi


        The main operational part of this script is



        player1_short=$( printf '%sn' "$player1_word" | sed "s/[$player2_word]//g" )
        player2_short=$( printf '%sn' "$player2_word" | sed "s/[$player1_word]//g" )


        which sets the two _short variables to the value of each _word variable with the letters from the other _word variable removed. This is done by using one of the the words in a bracketed expression ([...]) and doing a global substitution over the other word that removes each character that matches any character in the [...] expression.



        Testing:



        $ sh ./script.sh
        Player2 wins with "bxolq" ("bxq") over "hello" ("he")





        share|improve this answer














        Assuming you want to eliminate all characters that are the same in the two strings, you can do so in two calls to sed:



        #!/bin/sh

        player1_word='hello'
        player2_word='bxolq'

        player1_short=$( printf '%sn' "$player1_word" | sed "s/[$player2_word]//g" )
        player2_short=$( printf '%sn' "$player2_word" | sed "s/[$player1_word]//g" )

        if [ "$#player1_short" -gt "$#player2_short" ]; then
        printf 'Player1 wins with "%s" ("%s") over "%s" ("%s")n'
        "$player1_word" "$player1_short"
        "$player2_word" "$player2_short"
        elif [ "$#player2_short" -gt "$#player1_short" ]; then
        printf 'Player2 wins with "%s" ("%s") over "%s" ("%s")n'
        "$player2_word" "$player2_short"
        "$player1_word" "$player1_short"
        else
        printf 'It is a draw between "%s" ("%s") and "%s" ("%s")n'
        "$player1_word" "$player1_short"
        "$player2_word" "$player2_short"
        fi


        The main operational part of this script is



        player1_short=$( printf '%sn' "$player1_word" | sed "s/[$player2_word]//g" )
        player2_short=$( printf '%sn' "$player2_word" | sed "s/[$player1_word]//g" )


        which sets the two _short variables to the value of each _word variable with the letters from the other _word variable removed. This is done by using one of the the words in a bracketed expression ([...]) and doing a global substitution over the other word that removes each character that matches any character in the [...] expression.



        Testing:



        $ sh ./script.sh
        Player2 wins with "bxolq" ("bxq") over "hello" ("he")






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Sep 5 at 14:13

























        answered Sep 4 at 12:04









        Kusalananda

        107k14209331




        107k14209331



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f333972%2fleftover-characters-after-comparing-two-strings%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Christian Cage

            How to properly install USB display driver for Fresco Logic FL2000DX on Ubuntu?