Replace every other space

I'd like a sed command to replace every other space in my file.

File:

0 1 0 3 0 2 0 5

Want:

01 03 02 05

Does anyone have any ideas?

awk '{ print $1 $2, $3 $4, $5 $6} ' olfdile > newfile

1 Like

or along the similar lines:

echo '0 1 0 3 0 2 0 5' | nawk '{for(;++i<=NF;) printf("%s%c", $i, (i%2)?"":(i==NF)?ORS:OFS)}'
$ echo '0 1 0 3 0 2 0 5' | sed "s/[^ ]$/& /;s/ \([^ ]*\) /\1 /g"
01 03 02 05
echo '0 1 0 3 0 2 0 5' |awk '{for (i=1;i<=NF;i++) {printf ((i%2)?FS:S) $i}}' 
1 Like

And now, time for some Perl... :wink:

$ 
$ echo "0 1 0 3 0 2 0 5" | perl -ple 's/(\d) (\d)/$1$2/g'
01 03 02 05
$ 
$ echo "0 1 0 3 0 2 0 5" | perl -ple 's/(.) (.)/$1$2/g'
01 03 02 05
$ 

tyler_durden

Coming to think of it, sed should work with a similar regex as well...

$ 
$ echo "0 1 0 3 0 2 0 5" | sed 's/\(.\) \(.\)/\1\2/g'
01 03 02 05
$ 

If the input has more than one digit

$  echo '0 1 0 3 0 2 0 5' | sed "s/\([^ ]\{1,\}\) \([^ ]\{1,\}\)/\1\2/g"
01 03 02 05

A different solution:

echo '0 1 0 3 0 2 0 5' |awk '{for (i=0;++i<=NF;) {printf ((i-1 && i%2)?FS:S) $i}}'
echo '0 1 0 3 0 2 0 5'|sed 's/ //g;s/../& /g'

Thanks, I really appreciate it.