By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,264 Members | 1,743 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,264 IT Pros & Developers. It's quick & easy.

replacing two EOL chars by one

P: n/a
i have a bunch of java files that has spaced-out formatting that i
want to get rid of. I want to replace two end of line characters by
one end of line characters. The files in question is unix, and i'm
also working under unix, so i did:

perl -pi'*~' -e "s@\n\n@\n@g" *.java

but this to no avail.

after many minutes of checking and rechecking and lots of trial and
error with frustration, i realized that the fucking perl to my
expectations again cannot do this simple fucking thing. Nor can the
unix utility tr. Fucking stupid perl couldn't do a simple task of
replacing strings.

let me just take this opportunity to explain one shit from the
thousands from perldoc.
The following is a excerpt from perlre:

m Treat string as multiple lines. That is, change "^" and "$" from
matching the start or end of the string to matching the start or
end
of any line anywhere within the string.

s Treat string as single line. That is, change "." to match any
character whatsoever, even a newline, which normally it would not
match.

Note the first sentences of the two, which are logical opposites, but
in fact they are sinister and not opposite at all. Fucking stupid perl
documentation and Larry Wall moron who is i believe incapable of
writing clearly and logically in English masquerading as humor.

Now may i ask those who wallow in unix and perl: How do you do this?

(if the answer is to write a more elaborate program in perl that
explicitly open files and tread lines, please don't bother answering.
Unix util answer also welcome. (this message is cross posted to python
and scheme and ruby groups because i want them to know the
incompetence and sham aspects of perl. (or my opinions thereof.)))

Thanks.

Xah
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html
Jul 18 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Is this language really necessary? I'm sure I've used these words myself,
but they're certainly not appropriate in a public discussion.

I do not know the answer to your question, it really seems more like a
regexp question, rather than a problem with Perl. I hope someone who knows
the answer can respond, hopefully in more civil language.
Jul 18 '05 #2

P: n/a
Paul McGuire wrote:
I hope someone who knows the answer can respond, hopefully in more
civil language.


I'm sure that quite a few people know the answer, but most of them
have probably killfiled or decided to ignore him.

I hope that nobody provides an answer.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Jul 18 '05 #3

P: n/a
"Xah Lee" <xa*@xahlee.org> wrote in message
news:7f**************************@posting.google.c om...
i have a bunch of java files that has spaced-out formatting that i
want to get rid of. I want to replace two end of line characters by
one end of line characters. The files in question is unix, and i'm
also working under unix, so i did:

perl -pi'*~' -e "s@\n\n@\n@g" *.java

but this to no avail.

snipped long rant about perldocs

the problem is not your understanding of regular expressions,
but rather that you are forgetting what -p does
you are applying the -e commands to each line of input in turn before
printing it. no line of the input contained \n\n as then they would not
have been ONE line.

now, to do what you want, try:
perl -0 -pi'*~' -e 's@\n\n@\n@g' *.java


Jul 18 '05 #4

P: n/a

"Xah Lee" <xa*@xahlee.org> wrote:
i have a bunch of java files that has spaced-out formatting that i
want to get rid of. I want to replace two end of line characters by
one end of line characters. The files in question is unix, and i'm
also working under unix, so i did:

perl -pi'*~' -e "s@\n\n@\n@g" *.java

but this to no avail.
Here are two Ruby solutions, both of which have Perl equivalents:

ruby -i~ -e '$/=nil; puts gets.gsub(/\n\n/,"\n")' *.java

ruby -i~ -e '$/="\n\n"; while(gets)do puts chop end' *.java

You may also want to check out http://astyle.sourceforge.net/
which is a free source code beautifier for C/C++/Java.
after many minutes of checking and rechecking and lots of trial and
error with frustration, i realized that the fucking perl to my
expectations again cannot do this simple fucking thing. Nor can the
unix utility tr. Fucking stupid perl couldn't do a simple task of
replacing strings.

let me just take this opportunity to explain one shit from the
thousands from perldoc. [...]


http://www.wavlist.com/movies/135/str-francis.wav
"Lighten up, Francis."
Regards,

Bill

Jul 18 '05 #5

P: n/a
Gunnar Hjalmarsson <no*****@gunnar.cc> writes:
Paul McGuire wrote:
I hope someone who knows the answer can respond, hopefully in more
civil language.


I'm sure that quite a few people know the answer, but most of them
have probably killfiled or decided to ignore him.

I hope that nobody provides an answer.


And it's crossposted to numerous groups that have nothing to do with
perl.

--
Rahul Jain
rj***@nyct.net
Professional Software Developer, Amateur Quantum Mechanicist
Jul 18 '05 #6

P: n/a
Xah Lee wrote:
i have a bunch of java files that has spaced-out formatting that i
want to get rid of. I want to replace two end of line characters by
one end of line characters. The files in question is unix, and i'm
also working under unix, so i did:

perl -pi'*~' -e "s@\n\n@\n@g" *.java

but this to no avail.

after many minutes of checking and rechecking and lots of trial and
error with frustration, i realized that the fucking perl to my
expectations again cannot do this simple fucking thing. Nor can the
unix utility tr. Fucking stupid perl couldn't do a simple task of
replacing strings.

let me just take this opportunity to explain one shit from the
thousands from perldoc.

[rest of rant snipped]

Five seconds of thinking might have spared you from embarassing yourself
like that (and is certainly more effective than 5 paragraphs of swearing).
You are reading the file line by line. And with each line you are looking
for two consecutive newlines.
Now, how can a single line contain two newlines?

jue
Jul 18 '05 #7

P: n/a
xa*@xahlee.org (Xah Lee) wrote:

: i have a bunch of java files that has spaced-out formatting that i
: want to get rid of. I want to replace two end of line characters by
: one end of line characters. The files in question is unix, and i'm
: also working under unix, so i did:
:
: perl -pi'*~' -e "s@\n\n@\n@g" *.java
:
: but this to no avail.

Of course it's not.

Perl's -p switch processes one line at a time. A line cannot have more
than one newline character.

Perl's "paragraph" reading might be useful to you, where any number of
consecutive newline characters mark the end of a record. See "$/" in
perlvar for details.

perl -i'*~' -lpe "BEGIN{$/=''}" *.java

: after many minutes of checking and rechecking and lots of trial and
: error with frustration, i realized that the fucking perl to my
: expectations again cannot do this simple fucking thing.

Complete nonsense. Don't blame your own incompetence on Perl.

: Fucking stupid perl
: documentation and Larry Wall moron who is i believe incapable of
: writing clearly and logically in English masquerading as humor.

If you feel you can explaint it more clearly, quit bellyaching about it and
submit a documentation patch.

Preferrably one that is less petulant and profane than your article.
Frustration is no excuse for incivility.

Jul 18 '05 #8

P: n/a

xa*@xahlee.org (Xah Lee) writes:
i have a bunch of java files that has spaced-out formatting that i
want to get rid of. I want to replace two end of line characters by
one end of line characters. The files in question is unix, and i'm
also working under unix, so i did:


Have a look at tr and sed. tr is also a handy tool for fixing Windows
cr/lf madness.

Gregm
Jul 18 '05 #9

P: n/a
"Paul McGuire" <pt***@austin.rr.com> writes:
Is this language really necessary? I'm sure I've used these words myself,
but they're certainly not appropriate in a public discussion.
We don't get the usenet "in color" around here, so I guess that this
languages reflected the fuming hot in frustration state of the OP. I
guess that you could measure the quality of a design and its
documentations by the inverse of the number of posts related to it
using such language.

When we'll have video-usenet, I guess language will be more proper,
but we'll be seeing keyboads and screens flying out of the windows
more often.

I do not know the answer to your question, it really seems more like a
regexp question, rather than a problem with Perl. I hope someone who knows
the answer can respond, hopefully in more civil language.


Even with sed I find difficult to do that.
On the other hand, with emacs it's quite simple:

M-x replace-string RET C-q C-j C-q C-j RET C-q C-j RET

--
__Pascal_Bourguignon__ . * * . * .* .
http://www.informatimago.com/ . * . .*
There is no worse tyranny than to force * . . /\ ( . *
a man to pay for what he does not . . / .\ . * .
want merely because you think it .*. / * \ . .
would be good for him. -- Robert Heinlein . /* o \ .
http://www.theadvocates.org/ * '''||''' .
SCO Spam-magnet: po********@sco.com ******************
Jul 18 '05 #10

P: n/a
Pascal Bourguignon <sp**@thalassa.informatimago.com>:
Even with sed I find difficult to do that.


If you want to remove blank lines, then sed '/^$/d' will do. If, on
the other hand, you actually want to compress pairs of newlines into
single newlines (as I think the specification may have said), something
a bit more complicated is required. I think that sed 'N;P;/\n$/d;D'
will do it.

Jesse
Jul 18 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.