Removing extra spaces while preserving indents

Dan Kohn dan@dankohn.com
Mon, 24 Jun 2002 09:28:54 -0700


All kudos to Rob Harley for his awesome answer.  The trick was a) using
the [^] negation symbol: ^ in brackets means not but everywhere else
means beginning of a line and b) realizing that non-indent spaces are by
definition not preceded by a space.

You, sir, are a sed stud.  Thanks.

Of course, he gets even more points for showing that 'lynx -justify=3D0'
does what I want (even though the lynx man page doesn't show -justify
taking an argument).

So, Rob got me from this:

| lynx -dump -force_html -stdin \
| sed -e 's/^\ \ \ \ \ /bigindent/' \
| sed -e 's/\ \+/\ /g' \
| sed -e 's/^\ /\ \ \ /g' \
| sed -e 's/^bigindent/\ \ \ \ \ /' \
| mutt $MAILLIST -s "${SUBJ_}"

To this:

| lynx -dump -force_html -stdin \
| sed -e 's/\([^ ]\) \+/\1 /g'
| mutt $MAILLIST -s "${SUBJ_}"

To this:

| lynx -dump -force_html -stdin -justify=3D0 \
| mutt $MAILLIST -s "${SUBJ_}"

Thanks man.

          - dan
--
Dan Kohn <mailto:dan@dankohn.com>
<http://www.dankohn.com/>  <tel:+1-650-327-2600>

-----Original Message-----
From: Robert Harley [mailto:harley@argote.ch]=20
Sent: Monday, June 24, 2002 02:21
To: fork@xent.com
Cc: Dan Kohn
Subject: Re: Any sed/regex gurus around?


If I understand correctly, you should be able to just replace multiple
spaces, that follow a non-space, by a single space, like so:

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

Hope this helps,
  Rob.
     .-.
.-.
    /   \           .-.                                 .-.           /
\
   /     \         /   \       .-.     _     .-.       /   \         /
\
  /       \       /     \     /   \   / \   /   \     /     \       /
\
 /         \     /       \   /     `-'   `-'     \   /       \     /
\
            \   /         `-'                     `-'         \   /
             `-'                                               `-'