How to remove the "4 980 Raphael" space between numbers only

8

I have a String "980 Rafael" and I would like to know how I will remove only the spaces that are between the numbers in shell script.

    
asked by anonymous 14.03.2017 / 12:46

3 answers

5

Take a look at the code below, remove the first blank space found.

echo "4 980 Rafael" | sed '0,/[[:blank:]]/s/[[:blank:]]//'

[[:blank:]] is a POSIX regex class that removes whitespace, tabs ... for more information about this class, regex posix

output:

  

4980 Rafael

The Regex used works as follows:

0,/padrao/s/padrao/substituicao/

where padrao is what will be replaced and substituicao is what will replace, 0 at the beginning says that it is only the first occurrence.

    
14.03.2017 / 13:01
1

Analogously to the response from @Mathias but with awk inves sed :

echo "4 980 Rafael" | awk '{sub(" ","")}1'
    
15.03.2017 / 10:27
1

Give me an idea that the previous answers are only removing the first space (wherever it is) or be sed 's/ //'

I propose:

echo "...   4 434 Rafael" | sed -r 's/([0-9]) ([0-9])//'

that is: replace Num Num - > NumNum

    
16.03.2017 / 10:51