How can I get bash/zsh to change some text from "foo.foo.foo" to "foo foo foo" with a script/alias?
You can use tr command to convert character.
% echo "foo.foo.foo" | tr '.' ' '
foo foo foo
Using pure bash:
bash-3.2$ a='a.a.a'
bash-3.2$ echo "${a/./ }"
a a.a
bash-3.2$ echo "${a//./ }"
a a a
You can make a function and add to the end of your ~/.bashrc
, for example:
nodot() { echo "$1" | sed 's/\./ /g' ; }
usage example:
$ nodot foo.foo.foo
foo foo foo
You can use this function in zsh too, just add to your ~/.zshrc
instead.
Using Internal Field Separator (IFS)
variable:
bash-4.3$ old_ifs=$IFS
bash-4.3$ IFS="."
bash-4.3$ var="foo.foo.foo"
bash-4.3$ echo $var
foo foo foo
bash-4.3$ IFS=$old_ifs
This can be put nicely into a function:
split_dot()
{
string="$1"
if set | grep -q "IFS";
then
ifs_unset="false"
old_ifs=$IFS
else
ifs_unset="true"
fi
IFS="."
echo $string
if [ "$ifs_unset" == "true" ];
then
unset IFS
else
IFS=$old_ifs
fi
}
And run as so:
bash-4.3$ split_dot "foo.baz.bar"
foo baz bar