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

Asked

Viewed 537 times

8

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

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 blank spaces, 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, the 0 at first says it is only the first occurrence.

  • 1

    He wants to remove spaces only between numbers.

  • 1

    @Murillogoulart I will correct the answer.

1

It gives me the idea that the previous answers are only removing the first space (wherever you are) ie sed 's/ //'

I propose:

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

i.e.: replace Num Num -> Numnum

1

Analogous to @Mathias' reply but with awk inves of the sed:

echo "4 980 Rafael" | awk '{sub(" ","")}1'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.