Can sed replace one or more spaces with one space?

Can sed replace one or more spaces with one space?

Post by Levin Magrud » Mon, 28 Feb 1994 22:58:19



I want to turn input that looks like

levin     tty1
root      tty2
rebecca   tty4

to

levin tty1
root tty2
rebecca tty4

Can sed do this?  (My copy doesn't seem to use '+' to mean one or
more.)  (And if it varies, my input is filled with spaces, not
tabs.)

End of question proper.  Follow-up is set to me, I hope.  I'll
summarize.

Other questions:
1.  Would it be wiser to ask this in c.u.shell?
2.  I'm sure there are 40 ways other than sed to do this; if you
    send me other suggestions, I'll summarize.


 
 
 

Can sed replace one or more spaces with one space?

Post by Malcolm Mladenov » Tue, 01 Mar 1994 06:06:22


[note: trn thinks 'Poster' is different from 'poster' in a followup-to:
line; in any case there doesn't seem to be an MX record for 'ebo.ci.net'
anywhere, I did try mail first...]


Quote:>I want to turn input that looks like

> [remove duplicate spaces]

>Can sed do this?  (My copy doesn't seem to use '+' to mean one or
>more.)  (And if it varies, my input is filled with spaces, not
>tabs.)

sed uses ed regular expressions (nearly) and '+' does not mean
'one-or-more' in ed.  The following should work, though:

sed -e 's/  */ /g'

or, using a more general sed construct that's not realy needed in this
case, but possibly worth remembering:

sed -e 's/ \{1,\}/ /g'    (meaning 1 or more, \{1,10\} means 1 to 10, etc)

(the g should be omitted if only the first occurrence of multiple spaces
is to be replaced)

Quote:>Other questions:
>1.  Would it be wiser to ask this in c.u.shell?

probably, but there's not much in it.

Quote:>2.  I'm sure there are 40 ways other than sed to do this; if you
>    send me other suggestions, I'll summarize.

sed is probably the easiest, an alternative in sh (very specific
to this requirement) might be:

while read a rest
do
        echo "$a $rest"
done

There are certainly other ways (awk for example), but it's largely a
matter of personal taste unless specific constraints apply.

-Malcolm

 
 
 

Can sed replace one or more spaces with one space?

Post by David W. Tamk » Tue, 01 Mar 1994 14:15:57



| sed uses ed regular expressions (nearly) and '+' does not mean
| 'one-or-more' in ed.

The plus sign is magic in some versions of sed.

| The following should work, though:
|
| sed -e 's/  */ /g'

That's the general solution.  The following is not, because not all seds
understand the magic of escaped braces:

| or, using a more general sed construct that's not realy needed in this
| case, but possibly worth remembering:
|
| sed -e 's/ \{1,\}/ /g'    (meaning 1 or more, \{1,10\} means 1 to 10, etc)
|
| (the g should be omitted if only the first occurrence of multiple spaces
| is to be replaced)

It seems to me that ed doesn't recognize \{ \} either; are there actually
versions of sed where \{ \} are magic but + is not?  I'd expect that in any
given implementation of sed, either both would be or neither would be.

David W. Tamkin  P. O. Box 3284  Skokie IL  60076-6284

 
 
 

Can sed replace one or more spaces with one space?

Post by Malcolm Mladenov » Tue, 01 Mar 1994 22:05:52




>| sed uses ed regular expressions (nearly) and '+' does not mean
>| 'one-or-more' in ed.

>The plus sign is magic in some versions of sed.

Granted (I was a little surprised to find that it _wasn't_ in the versions
I have available, I'd just never used it before).

Quote:>| The following should work, though:
>|
>| sed -e 's/  */ /g'

>That's the general solution.  The following is not, because not all seds
>understand the magic of escaped braces:

XPG3 requires it as part of /Simple Regular Expressions/.  I realise this is
not a universal requirement but many systems are now XPG3 conformant and
more are becoming so all the time.  However, I agree that it can't be
relied on if portability is important.

Quote:>| or, using a more general sed construct that's not realy needed in this
>| case, but possibly worth remembering:
>|
>| sed -e 's/ \{1,\}/ /g'    (meaning 1 or more, \{1,10\} means 1 to 10, etc)
>|
>| (the g should be omitted if only the first occurrence of multiple spaces
>| is to be replaced)

>It seems to me that ed doesn't recognize \{ \} either; are there actually
>versions of sed where \{ \} are magic but + is not?  I'd expect that in any
>given implementation of sed, either both would be or neither would be.

SVR4.x supports braces but not + in sed (and ed).  Note that XPG3 requires
ed to support braces (since it uses /Simple Regular Expressions/) as well.
Magic '+' is included as part of /Extended Regular Expressions/.

-Malcolm

 
 
 

Can sed replace one or more spaces with one space?

Post by Harald Hanche-Ols » Wed, 02 Mar 1994 21:30:27



Quote:Magruder) writes:

Levin> 2.  I'm sure there are 40 ways other than sed to do this; if you
Levin>     send me other suggestions, I'll summarize.

There is always this:

  tr -s ' ' ' '

(GNU tr treats tr -s ' ' the same way, but at least the SunOS supplied
 one does not.)


No, mail to that address bounces.  You ought to check that out:

550 ebo.ci.net (TCP)... 550 Host unknown

- Harald

 
 
 

1. Replacing spaces with "_" in first n characters using SED

'Lo all,

I am trying to parse lastb output, I would normally use awk to print the
approriate lines eg :

lastb | awk -F" " '{ if ( $4 == "'$Month'" ) print $5" "$6" "$1}'

but if the first field in the line contains a space or spaces the field
numbering awk expects is changed and the $4 = $month comparison cannot work.
I thought I could use sed to preprocess each line before it goes to awk.
But I need a command/regexp to replace any spaces there may be only in the
first 8 characters of the line with something else (probably underscores). I
cannot find anything in my books or the web to indicate how/if it can be
done.

Any ideas?

--
Jonathan Leckie, Please remove the Z in my email address to reply

2. useradd (Solaris 2.4)

3. shrink continuous space to one space

4. why linux-sun-jdk13 ?

5. one to one user-space thread and lwp

6. Telnetting to a UUCP daemon

7. Replacing multiple spaces with a single space

8. Installing SuSE 6.4 above 1024

9. sed command to replace text with blank spaces

10. Sed - replacing spaces in a string

11. Replacing dot with space using sed

12. replace space use sed

13. SED: search and replace multiple space???