Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I can maybe use < or > or |. Maybe I need to use grep?

share|improve this question
1  
What do you mean by a text? a file or some text in a file? echo "foo.foo.foo" | sed 's/\./ /g' – Zanna yesterday
1  
@K.D.G i) you need to create a separate account for each SE site, but if you use the same credentials, the accounts will be linked. ii) While this site is indeed limited to Ubuntu exclusively, I don't see anything here that would be irrefutable proof that you are not using Ubuntu, so I also se no reason to close it. – terdon yesterday
up vote 14 down vote accepted

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.

share|improve this answer
    
@tbodt changing an accepted answer to completely replace it with another current answer is simply not done. – muru 1 hour ago

You can use tr command to convert character.

% echo "foo.foo.foo" | tr '.' ' ' 
foo foo foo
share|improve this answer
    
cool tips never hear of "tr" before – K.D.G yesterday
    
@K.D.G Thanks :) Btw, I would highly suggest to you to read few books. You can start from the following one: amazon.com/Linux-Command-Line-Complete-Introduction/dp/… – Validus Oculus yesterday
4  
tr is actually meant for exactly this purpose. sed is overkill. – Max Ried yesterday
1  
+1 precisely for tr, which is a very useful tool that a lot of people don't seem to know about for some reason. Way simpler than sed for converting entire classes of characters. – fluffy yesterday
2  
Never write it in 'C' if you can do it in 'awk'; Never do it in 'awk' if 'sed' can handle it; Never use 'sed' when 'tr' can do the job; Never invoke 'tr' when 'cat' is sufficient; Avoid using 'cat' whenever possible. --Taylor's Laws of Programming – Ross Presser 22 hours ago

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
share|improve this answer

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
share|improve this answer
2  
Resetting IFS is a more tricky than just saving it to a variable and restoring the value. An unset IFS and an empty IFS have different behaviour, so you need to check whether it is unset or just empty, in case it was empty before. – muru yesterday
    
@muru since functions run in subshell, i don't see how it can be unset unless you explicitly do so. please explain – Serg yesterday
3  
Functions don't run in subshells. Only some variables have different scope (the positional parameters, and a few others in listed in gnu.org/software/bash/manual/html_node/Shell-Functions.html)‌​. That's why we have the local keyword. – muru yesterday
2  
So if OP had unset IFS for some reason previously, this function would set it to empty, which does have different behaviour. – muru yesterday
2  
@fedorqui you might find my similar question on U&L interesting: unix.stackexchange.com/q/264635/70524 (Gilles always gives very good answers) – muru yesterday

Anyone missed awk/perl/python/go:


% awk '{gsub(/[.]/, " ", $0); print}' <<<'foo.foo.foo'                      
foo foo foo

% perl -pe 's/\./ /g' <<<'foo.foo.foo'                                      
foo foo foo

% python -c 'import sys; print sys.stdin.read().replace(".", " ")' <<<'foo.foo.foo'
foo foo foo

% cat input.go
package main

import (
    "os"
    "strings"
    "fmt"
)

func main () {
    args := os.Args
    if len(args) != 2 {
        fmt.Println("Not enough arguments")
        return
    }
    out := strings.Replace(args[1], ".", " ", -1)
    fmt.Println(out)
}

% go run input.go 'foo.foo.foo' 
foo foo foo
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.