Reputation: 211
Using sed, how can I replace 2 or more white spaces with NULL?
Input
200 0 0 100 300 400 10
Desired output
200 0 0100300400 10
Upvotes: 1
Views: 268
Reputation: 17198
Pure Bash. In a script you could use a parameter substitution:
str="200 0 0 100 300 400 10"
shopt -s extglob
str=${str// +( )/}
echo "$str"
200 0 0100300400 10
The substitution ${str// +( )/}
deletes all whitespaces followed by at least one whitespace.
Upvotes: 0
Reputation: 77185
Posting here for reference, an awk
solution:
$ echo '200 0 0 100 300 400 10' | awk '{gsub(/ +/,"")}1'
200 0 0100300400 10
Upvotes: 0
Reputation: 123668
You could use sed
(this is not specific to GNU sed):
sed -r 's/[ ]{2,}//g' filename
or (without -r
):
sed 's/[ ]\{2,\}//g' filename
For your input, this would produce:
200 0 0100300400 10
Upvotes: 5
Reputation: 204638
This will work with any sed, not just GNU sed:
sed -e 's/ *//g'
Upvotes: 5