ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Main Area and Open Discussion > General Software Discussion

Search files that contain trailing spaces

(1/1)

blackcat:
Is this possible? Can we search for all text files that contain trailing spaces and batch remove those trailing space in all files? Yea using regex, if that is the only way to do it. And also regex rules to replace those spaces. I think im going to use "Find in files" feature in notepad++  :D

Any idea or suggestions?

tranglos:
The search regex would be something like
[ ]+$

(the square brackets aren't required, but help visualize the space character).

Tested in Total Commander, but should work for any regular expression engine. If you need to account for tabs as well, in TC you could use
\s+$

TC does not support a whitespace special character, but many regex search engines do. In TextPad, for example, you could search for
[:blank:]

The metod of stripping the trailing blanks though will depend on the particular program and its implementation of regular expressions. Usually the help file will have examples of search and replace operations. (I use searching heavily, but almost never do automated replace, since it can wreck a week's worth of my work in two seconds. I'm sure others will post with more suggestions.)

And if the number of files is small, I would simply use a text editor with an option to strip trailing blanks when saving (e.g. TextPad), open all the files at once and save them. For up to a dozen files or so, that would be faster than reading the manual on regexes :)

blackcat:
Very helpful tips tranglos

So \s+$ includes both trailing spaces and tabs, right?

What's the different with [ \t]+$

tranglos:
So \s+$ includes both trailing spaces and tabs, right?
-blackcat (April 23, 2009, 08:45 AM)
--- End quote ---

That's right. It's the same as [:blank:]+$ - but this latter syntax, the so called POSIX notation, is not always supported.


What's the different with [ \t]+$
-blackcat (April 23, 2009, 08:45 AM)
--- End quote ---

No difference again, although \s would theoretically match any other blank characters, if they existed :)

The real difference is in what a particular application supports. I've just found out for example that TotalCommander supports \s, but for some reason does not seem to understand \t - even though the help file says it does. I'll try again and report a bug if confirmed.

Another area where apps differ is in the interpretation of line breaks. In TextPad, a linebreak is \n, so the regex could also be written as
[ \t]+\n

and you would replace that with
\n

In other text editors though \n may only mean the newline character. As you may know, in DOS and Windows the linebreak actually consists of two characters: caret return and new line. So some apps require you to spell them out:
[ \t]+\r\n

and you would replace that with
\r\n



Navigation

[0] Message Index

Go to full version