threads / discuss / 4302

[SCRIPT] chomp: trim trailing whitespace

Subject: [SCRIPT] chomp: trim trailing whitespace

## tl;dr

11 messages between May 27, 2006 and May 28, 2006.

replies: 10people: 9as markdown or json

Jeff Garzik· May 27, 2006, 02:27 UTC · lore

Attached to this email is chomp.pl, a Perl script which removes trailing whitespace from several files. I've had this for years, as trailing whitespace is one of my pet peeves.

Now that git-applymbox complains loudly whenever a patch adds trailing whitespace, I figured this script may be useful to others.

	Jeff
Jan Engelhardt· May 27, 2006, 10:15 UTC · re: Jeff Garzik · lore

Re: [SCRIPT] chomp: trim trailing whitespace

Show 7 quoted lines
> Attached to this email is chomp.pl, a Perl script which removes trailing
> whitespace from several files.  I've had this for years, as trailing whitespace
> is one of my pet peeves.
>
> Now that git-applymbox complains loudly whenever a patch adds trailing
> whitespace, I figured this script may be useful to others.
>

Pretty long script. How about this two-liner? It does not show 'bytes chomped' but it also trims trailing whitespace.

#!/usr/bin/perl -i -p s/[ \t\r\n]+$//

Jan Engelhardt
Neil Brown· May 27, 2006, 10:36 UTC · re: Thomas Glanzmann · lore

Re: [SCRIPT] chomp: trim trailing whitespace

On Saturday May 27, sithglan@stud.uni-erlangen.de wrote:
Show 7 quoted lines
> Hello,
> 
> > #!/usr/bin/perl -i -p
> > s/[ \t\r\n]+$//
> 
> perl -p -i -e 's/\s+$//' file1 file2 file3 ...
> 

Uhm... have either of you actually tried those? When I tried, I lose all the '\n' characters :-(

  perl -pi -e 's/[ \t\r]+$//'  *.[ch]
seems to actually work.
NeilBrown
Jeff Garzik· May 27, 2006, 11:32 UTC · re: Jan Engelhardt · lore

Re: [SCRIPT] chomp: trim trailing whitespace

Jan Engelhardt wrote:
Show 13 quoted lines
>> Attached to this email is chomp.pl, a Perl script which removes trailing
>> whitespace from several files.  I've had this for years, as trailing whitespace
>> is one of my pet peeves.
>>
>> Now that git-applymbox complains loudly whenever a patch adds trailing
>> whitespace, I figured this script may be useful to others.
>>
> 
> Pretty long script. How about this two-liner? It does not show 'bytes 
> chomped' but it also trims trailing whitespace.
> 
> #!/usr/bin/perl -i -p
> s/[ \t\r\n]+$//
Yes, it does, but a bit too aggressive for what we need :)
	Jeff
Dmitry Fedorov· May 27, 2006, 11:48 UTC · re: Jeff Garzik · lore

Re: [SCRIPT] chomp: trim trailing whitespace

Jan Engelhardt wrote:
>> Attached to this email is chomp.pl, a Perl script which removes trailing
>> whitespace from several files.  I've had this for years, as
trailing whitespace
>> is one of my pet peeves.
And my scripts.
Jan Engelhardt· May 27, 2006, 12:42 UTC · re: Jeff Garzik · lore

Re: [SCRIPT] chomp: trim trailing whitespace

Show 8 quoted lines
>> Pretty long script. How about this two-liner? It does not show 'bytes
>> chomped' but it also trims trailing whitespace.
>> 
>> #!/usr/bin/perl -i -p
>> s/[ \t\r\n]+$//
>
> Yes, it does, but a bit too aggressive for what we need :)
>
Whoops, should have been s/[ \t\r]+$//
And the CL form is
  perl -i -pe '...'
Somehow, you can't group it to -ipe, but who cares.
Jan Engelhardt
Keith Owens· May 28, 2006, 08:33 UTC · re: Jan Engelhardt · lore

Re: [SCRIPT] chomp: trim trailing whitespace

Jan Engelhardt (on Sat, 27 May 2006 14:42:02 +0200 (MEST)) wrote:
>And the CL form is
>  perl -i -pe '...'
>Somehow, you can't group it to -ipe, but who cares.

-i takes an optional extension which is used to optionally create backup files. As such, -i must be followed by space if you want no extension (and no backup).

Martin Langhoff· May 27, 2006, 15:28 UTC · re: Jeff Garzik · lore

Re: [SCRIPT] chomp: trim trailing whitespace

I love perl golf for this kind of stuff... but git-stripspace is part of git already. Even then, I tend to do it with perl -pi -e '' constructs ;-)

cheers,
m
Linus Torvalds· May 27, 2006, 16:13 UTC · re: Martin Langhoff · lore

Re: [SCRIPT] chomp: trim trailing whitespace

On Sun, 28 May 2006, Martin Langhoff wrote:
>
> I love perl golf for this kind of stuff... but git-stripspace is part
> of git already. Even then, I tend to do it with perl -pi -e ''
> constructs ;-)

Well, git-stripspace actually does something slightly differently, in that it also removes extraneous all-whitespace lines from the beginning, the end, and the middle (in the middle, the rule is: two or more empty lines are collapsed into one).

Ie it's a total hack for parsing just commit messages (and it is in C, because I can personally write 25 lines of C in about a millionth of the time I can write 3 lines of perl).

		Linus
Johannes Schindelin· May 28, 2006, 10:00 UTC · re: Linus Torvalds · lore

Re: [SCRIPT] chomp: trim trailing whitespace

Hi,
On Sat, 27 May 2006, Linus Torvalds wrote:
Show 8 quoted lines
> Well, git-stripspace actually does something slightly differently, in that 
> it also removes extraneous all-whitespace lines from the beginning, the 
> end, and the middle (in the middle, the rule is: two or more empty lines 
> are collapsed into one).
> 
> Ie it's a total hack for parsing just commit messages (and it is in C, 
> because I can personally write 25 lines of C in about a millionth of the 
> time I can write 3 lines of perl).

But there is no good reason not to add some code and a command line switch, so that this tool with a very generic name actually performs what a normal person would expect from that name.

Ciao, Dscho

← back to recent threads