Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Trailing Whitespace

Name: Anonymous 2012-04-05 7:07

Hey everyone,

A few months ago, I posted my Python script to remove trailing whitespace from a file. Somebody responded with a Perl 1-line code that does the same thing.

I could really use that Perl code now, anybody? Thanks.

Name: Anonymous 2012-04-05 7:16

You could do the same thing in Lisp.

1) If it ain't Lisp, it's crap.
2) Lisp is shit.

Name: Anonymous 2012-04-05 7:19

>>1
I don't know about Perl, but here's how you do it in Vim: :%s/\s\+$//

Name: Anonymous 2012-04-05 7:27

>>3
Yeah I know that, I have it ran when I save a file, but I need to automatize it with Bash, Perl, or Python. And I don't want to loop all the lines and remove space.

Name: Anonymous 2012-04-05 7:54

sed 's/[ \t]*$//'

Name: Anonymous 2012-04-05 7:58

contents = open(filename).read().rstrip()
open(filename, 'w').write(contents)

Name: Anonymous 2012-04-05 8:06


perl -e 'open F1, "file.txt"; open F2, ">out.txt"; while (<F1>) { s/\s+$//g; print F2 $_.$/; } close F2;'

Name: Anonymous 2012-04-05 8:06

>>6


>>> a = "lol  \n  \n\nlol"
>>> a.rstrip()
'lol  \n  \n\nlol'


The trailing spaces seem to be there.

>>5
How can I use that, do I pass an argument or something? Thanks.

Name: Anonymous 2012-04-05 8:14

>>8
I assumed trailing at the end of the file.

Name: Anonymous 2012-04-05 8:17

>>5,7
That perl does that job, but for that task i think that sed is the right tool for the job.

Personally, except for quick hacks that are used one or two times, I avoid to use shell scripts at all and only write perl.

Name: Anonymous 2012-04-05 8:18

>>8,9
I didn't test this, but in typical Pythonista fashion.

contents = [line.rstrip() for line in open(filename)]
open(filename, 'w').write('\n'.join(contents))

Name: Anonymous 2012-04-05 9:12

>>7
Are you mega retard perl noob? Get out of here.

perl -ne 's/\s+$//; print $_, "\n";'

Name: Anonymous 2012-04-05 10:10

>>12
I prefer to write verbose perl. Specially when showing "how to do X". Golfing is not a "feature".

Name: Anonymous 2012-04-05 11:20

>>8
Damn, bitch, why you no RTFM?

sed -i 's/[ \t]*$//' *.cpp

Name: Anonymous 2012-04-06 0:08

>>13
Yet the >>12 version is actually useful and more featureful supporting command line arguments as well as reading from stdin plus it writes to stdout which is the sensible thing to do.
You could've used an explicit while (<>) if you wanted to be didactic or some gay bullshit. That's a Perl idiom anyway. Not using it just shows you're clueless.
It's not like your intent becomes any clearer with boilerplate like open.

Name: Anonymous 2012-04-06 21:37

>>5
>>10
>>12
>>14
sed does this so well that Perl even includes syntax that emulates sed.  You'd be crazy to use anything else.

Name: Anonymous 2012-04-06 22:20

>>7
Cool temporary file, ``faggot''

Name: Anonymous 2012-04-06 23:02

>>16
anything else you say?


lua -e "for line in io.lines() do io.write(({line:gsub('%s*$', '')})[1]..'\n') end"

Name: Anonymous 2012-04-07 1:00

>>18
I'm with >>16 the Perl version isn't half that ugly.

Name: Anonymous 2012-04-07 1:19

yeah, actually having regex built in to the syntax is nice. But how about this?


(define (map-lines fn input output)
  (let ((line (read-line input)))
    (if (not (eof-object? line))
      (begin (display (fn line) output)
             (newline output)
             (map-lines fn input output)))))

(define (map-input fn)
  (map-lines fn (current-input-port) (current-output-port)))

(map-input (lambda (x) (string-edit "\\s*$" "" x)))

Name: Anonymous 2012-04-07 1:50

>>20
)))))

Name: Anonymous 2012-04-07 2:14

>>21
could be worse

Name: Anonymous 2012-04-07 2:22

>>21

but in any case:


(define (map-lines fn input output)
  (let ((line (read-line input)))
    (if (not (eof-object? line))
      (begin (display (fn line) output)
             (newline output)
             (map-lines fn input output)
      )
    )
  )
)

Name: Anonymous 2012-04-07 5:44

1Press CTRL+H to open Microsoft Visual Studio Enterprise Team Edition 2013TM "Search and replace" dialog.
2In "Search for:" box, enter ":b+$" (without quotes)
3Leave "Replace with:" box empty
4Select "Regular expressions" in the drop-down box under "Find options:"
5Click "Replace all"
6Masturbate furiously
7ENTERPRISE

Name: bampu pantsu 2012-05-29 4:11

bampu pantsu

Don't change these.
Name: Email:
Entire Thread Thread List