Custom string space manipulation with sed

This should work:

sed 's/ \([^ ]\)/\1/g'

You can also replace the whole last line of the script with this:

sed -n '1s/ \([^ ]\)/\1/gp' "$FILE"

How about this:

$ head -n1 $FILE | tail -n 1 | sed 's,  ,|,g' | sed 's, ,,g' | sed 's,|, ,g'
1234 23 345 456789

First we replace all occurrences of double whitespace with | in order to distinguish them from single whitespaces, then we remove all single whitespaces, and finally we replace | with a single whitespace.


You can do the replacements in a single s command in sed:

s/ \( \?\)/\1/g

To exit after the first line, we can q on line 1. Putting that together:

sed -e 's/ \( \?\)/\1/g' -e '1q'