Languages and Scripting

Single white space replacement whilst ignoring multiple spaces

 
SOLVED
Go to solution
eric lipede_1
Regular Advisor

Single white space replacement whilst ignoring multiple spaces

Hi
does anyone know how to replace a single white space and leave and multiple spaces alone....
so, for example:

echo "12 3 4 5 6"

should produce

12 34 56

(where the only single spaces were between 3 &4 and 5& )

awk/sed or perl is fine thks - preferable in one line - thks

8 REPLIES 8
Steven Schweda
Honored Contributor

Re: Single white space replacement whilst ignoring multiple spaces

Perhaps:

$ echo '12 3 4 5 6' | \
sed -e 's/\([^ ]\) \([^ ]\)/\1\2/g'
12 34 56

(Replace non-space1+space+non-space2 with
non-space1+non-space2 everywhere.)
James R. Ferguson
Acclaimed Contributor

Re: Single white space replacement whilst ignoring multiple spaces

Hi Eric:

A bit late, but in Perl:

echo "12 3 4 5 6"|perl -ple 's/(\S)\s(\S)/$1$2/g'
12 34 56

The '\s' is a whitespace (space, tab) character whereas the '\S' is a non-whitespace character.

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor
Solution

Re: Single white space replacement whilst ignoring multiple spaces

IMHO this would be an excellent example for using the \b 'boundary' matching in perl's regulare expression:

# echo "12 3 4 5 6"|perl -ple 's/\b\s\b//g'
12 345 6

(12ss3s4s5ss6)

Please observe how this replaced ALL single-white-space instances.

JRF's solution, would replace the single space between 3 and 4, but would NOT replace the single space between 4 and 5 for the example I used, as the match engine has already dealt with '4'.

You have to decide what is 'right' for your usage. I happen to think the using \b is more true to the original problem statement.

fwiw,
Hein


# echo "12 3 4 5 6"|perl -ple 's/(\S)\s(\S)/$1$2/g'
12 34 5 6
eric lipede_1
Regular Advisor

Re: Single white space replacement whilst ignoring multiple spaces

Thks to all of you.

Hein, I think your solution was the most robust
Dennis Handly
Acclaimed Contributor

Re: Single white space replacement whilst ignoring multiple spaces

>Hein: JRF's solution, would replace the single space between 3 and 4, but would NOT replace the single space between 4 and 5 for the example I used

It seems Steven's fails the same way. But one solution is simple, do it again:
echo '12 3 4 4 5 6' | sed -e 's/\([^ ]\) \([^ ]\)/\1\2/g' -e 's/\([^ ]\) \([^ ]\)/\1\2/g'
eric lipede_1
Regular Advisor

Re: Single white space replacement whilst ignoring multiple spaces

I re-opened this thread as I think that this is an interesting problem ...and of course to assign points to great answers....
James R. Ferguson
Acclaimed Contributor

Re: Single white space replacement whilst ignoring multiple spaces

Hi (again) Eriic:

> I re-opened this thread as I think that this is an interesting problem ...

From a Perl perspective, I think Hein provided the most general, most direct solution. It works for the original cases as well as Dennis's sample data.

Dennis does offer an interesting solution, though. My original offering of:

# perl -ple 's/(\S)\s(\S)/$1$2/g'

...could be modified to perform the repetitive substitutions of Dennis's 'sed' by writing:

# echo "12 3 4 4 5 6"|perl -ple '1 while s/(\S)\s(\S)/$1$2/g'
12 344 56

Regards!

...JRF...

eric lipede_1
Regular Advisor

Re: Single white space replacement whilst ignoring multiple spaces

Hi

Ive tested it with more variations of the sample data...and files . Both remove white spaces and ignore multiple instances of them -thks!