Leftover characters after comparing two strings
Clash 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?
text-processing string
add a comment |Â
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?
text-processing string
1
puzzles you how? what did you expect, and what did you get? You may findcomm
more useful here e.g. to get an ordered list of common characterscomm -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 wantsort -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 thato
is also in common; was it not removed because it's in a different position?
â Jeff Schaller
Apr 13 at 16:03
add a comment |Â
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?
text-processing string
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
text-processing string
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 findcomm
more useful here e.g. to get an ordered list of common characterscomm -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 wantsort -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 thato
is also in common; was it not removed because it's in a different position?
â Jeff Schaller
Apr 13 at 16:03
add a comment |Â
1
puzzles you how? what did you expect, and what did you get? You may findcomm
more useful here e.g. to get an ordered list of common characterscomm -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 wantsort -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 thato
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
add a comment |Â
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")
add a comment |Â
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")
add a comment |Â
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")
add a comment |Â
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")
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")
edited Sep 5 at 14:13
answered Sep 4 at 12:04
Kusalananda
107k14209331
107k14209331
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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 characterscomm -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