To reverse the effect of your command AND keeping the same part of the string to be matched (assuming that this is the only fix part of the string and you want to get rid of any other part that may vary), you can do :
echo "hello world, I am just simple text for display." | sed 's/^.*\(, I am.*\)/\1/g'
Result :
, I am just simple text for display
What the regular expression is doing :
- ^.* : matching any character at the beginning of the string until the next expression
- \( & \) : to catch the part of the string which is matched by the expression in between (must be escaped by \ or they will match the parenthesis character).
- , I am.* : the match you give us,
- \1 : will be replaced by the result of the match of the first sub-expression between parenthesis
This way, you have the exact reverse effect of the command in your question, using the same part of the string to do the match.
The main problem is here:
read $input
In bash, usually, $foo
is the value of the variable foo
. Here, you don't want the value, but the name of the variable, so it should be just:
read input
Similarly, in the if
tests, $yes
and $no
should be just yes
and no
, since you just want the strings yes
and no
there.
You could use a case
statement here, which (IMHO) makes it easier to do multiple cases based on input:
case $input in
[Yy]es) # The first character can be Y or y, so both Yes and yes work
echo "Hello!"
echo "Hello!" | festival --tts
;;
[Nn]o) # no or No
echo "Are you sure?"
echo "Are you sure?" | festival --tts
;;
*) # Anything else
echo "Please answer yes or no."
echo "Please answer yes or no." | festival --tts
;;
esac
You could wrap the two echo
statements and the use of festival
in a function to avoid repeating yourself:
textAndSpeech ()
{
echo "$@"
echo "$@" | festival --tts
}
case $input in
[Yy]es) # The first character can be Y or y, so both Yes and yes work
textAndSpeech "Hello!"
;;
[Nn]o) # no or No
textAndSpeech "Are you sure?"
;;
*) # Anything else
textAndSpeech "Please answer yes or no."
;;
esac
With $input
, bash replaces this with its value, which is nothing initially, so the read
command run is:
read
And read
by default stores the input in the variable REPLY
. So you can, if you want, eliminate the input
variable altogether and use $REPLY
instead of $input
.
Also have a look at the select
statement in Bash.
Best Answer
If you use
it will work correctly.
Probably the input of echo is separated by newlines, and it will handle it as separate commands, so the result will be without newlines.