Joy Jyoti
Joy Jyoti

Reputation: 211

Replace 2 or more spaces in a file with NULL

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

Answers (5)

Fritz G. Mehner
Fritz G. Mehner

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

Zsolt Botykai
Zsolt Botykai

Reputation: 51693

sed 's/  \+//g' INPUTFILE

will work (if you have GNU sed).

Upvotes: 4

jaypal singh
jaypal singh

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

devnull
devnull

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

Ed Morton
Ed Morton

Reputation: 204638

This will work with any sed, not just GNU sed:

sed -e 's/   *//g'

Upvotes: 5

Related Questions