how to delete spaces at the end of line?

3,144 views
Skip to first unread message

tobto

unread,
Sep 20, 2013, 4:14:34 AM9/20/13
to textwr...@googlegroups.com
I like to sort and remove duplicates from vertical list, but TW see lines with white spaces at the end as similar:

apple[ ]
apple[  ] 
apple[   ] 

Tom Humiston

unread,
Sep 20, 2013, 4:01:13 PM9/20/13
to textwr...@googlegroups.com
Here are two ways to remove duplicate lines without being tripped up by varying amounts of trailing white space:

The most direct way is to ignore them. From the Text menu, choose "Process Duplicate Lines..." and match using this search pattern:
^.*\S

Everything on a line up until the the last non-space character (\S) will be considered when comparing duplicates. The rest of the line will be ignored for comparison purposes because it falls outside what's matched by the pattern (a key concept not mentioned in the manual).

Alternatively, you can first remove all those bothersome trailing white spaces and then process duplicate lines without a match pattern. To remove all the trailing spaces, search for
[ ]+$
and replace with nothing. (The brackets here aren't necessary; they just show that the pattern has a space in it.)

- TH

David Brostoff

unread,
Sep 20, 2013, 4:03:15 PM9/20/13
to textwr...@googlegroups.com
My very low-tech way of doing this is to Find "[space]\r" and Replace All with "\r", repeating it three times.

David

Christopher Bort

unread,
Sep 20, 2013, 6:43:54 PM9/20/13
to textwr...@googlegroups.com
What if some lines have more than three trailing spaces? Better
to use a grep pattern that matches any number of white space
characters at the end of a line, like what Tom Humiston
suggested, so that you only have to do one Replace All. I might
change Tom's pattern from

[ ]+$

to

\s+$

in order to catch all white space characters rather than just
the space character itself, in the event that there might be
tabs at the ends of some lines. If fact, I have this exact
pattern in my saved search patterns for this very purpose.
--
Christopher Bort
<top...@thehundredacre.net>

David Brostoff

unread,
Sep 20, 2013, 7:29:15 PM9/20/13
to textwr...@googlegroups.com
On Sep 20, 2013, at 15:43 , Christopher Bort <top...@thehundredacre.net> wrote:

> What if some lines have more than three trailing spaces? Better to use a grep pattern that matches any number of white space characters at the end of a line, like what Tom Humiston suggested, so that you only have to do one Replace All. I might change Tom's pattern from
>
> [ ]+$
>
> to
>
> \s+$
>
> in order to catch all white space characters rather than just the space character itself, in the event that there might be tabs at the ends of some lines. If fact, I have this exact pattern in my saved search patterns for this very purpose.

Yes--as I said, my solution was very clumsy, and I regretted having posted it to such a sophisticated list almost as soon as I sent it. (If I had seen Tom Humiston's post first I wouldn't have posted at all.) I only thought it might help the OP if he was in a hurry and didn't get an answer from someone more knowledgeable right away.

David

Steve

unread,
Sep 21, 2013, 1:05:33 AM9/21/13
to textwr...@googlegroups.com, top...@thehundredacre.net

I might change Tom's pattern from

[ ]+$

to

\s+$


The only issue I have with '\s' as a character is that it also gobbles up newline characters with my preferences. Most of the text I use will have either a space or tab characters, so I've just found it easiest to use:

[\t ]+$ (there is a space in the brackets as well as the '\t' tab).

Of course, TextWrangler also has a "trim trailing spaces" option on save so if you don't want to manually search for those lines each time, you can just save the document normally, and all that whitespace gets removed. This is an option available system-wide, or language specific.

-Steve

Patrick Woolsey

unread,
Sep 21, 2013, 12:15:57 PM9/21/13
to textwr...@googlegroups.com
I see you've already gotten a bunch of good suggestions, and here's another
suggestion from a different angle.

You could also turn on the "Strip trailing whitespace" option in the Text
Files preference panel, and save the document to apply this option before
processing its contents for duplicates, etc.

The advantage of this method is that once you've set that option, it will
continue to apply to all the files that you work with.


For reference, here's page 187 of the PDF manual:
====
Strip trailing whitespace

When this option is on, TextWrangler will trim all trailing non-vertical
whitespace from the document [] before writing it out.

You can also adjust this option on a per-language basis by adding custom
language preferences. (See "Languages Preferences" on page 182).
====


Regards,

Patrick Woolsey
==
Bare Bones Software, Inc. <http://www.barebones.com/>

Tom Humiston

unread,
Sep 23, 2013, 11:31:27 AM9/23/13
to textwr...@googlegroups.com
On Sep 21, 2013, at 12:15 PM, Patrick Woolsey wrote:

> At 01:14 -0700 09/20/2013, tobto wrote:
>> I like to sort and remove duplicates from vertical list, but TW see lines
>> with white spaces at the end as similar:
>>
>> apple[ ]
>> apple[ ]
>> apple[ ]
>>
>
> I see you've already gotten a bunch of good suggestions, and here's another suggestion from a different angle.
>
> You could also turn on the "Strip trailing whitespace" option in the Text Files preference panel, and save the document to apply this option before processing its contents for duplicates, etc.


Good to know. I'd seen that pref but assumed it stripped space from the end of the document, not each line.

- Tom
Reply all
Reply to author
Forward
0 new messages