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.
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.
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
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 linux shell shell-script
You are not signed in. Login or sign up in order to post.
He wants to remove spaces only between numbers.
– Murillo Goulart
@Murillogoulart I will correct the answer.
– Mathiasfc