replacing two EOL chars by one

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Xah Lee

    replacing two EOL chars by one

    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
    xah@xahlee.org

  • Paul McGuire

    #2
    Re: replacing two EOL chars by one

    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.


    Comment

    • Gunnar Hjalmarsson

      #3
      Re: replacing two EOL chars by one

      Paul McGuire wrote:[color=blue]
      > I hope someone who knows the answer can respond, hopefully in more
      > civil language.[/color]

      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

      Comment

      • Ragnar Hafstað

        #4
        Re: replacing two EOL chars by one

        "Xah Lee" <xah@xahlee.org > wrote in message
        news:7fe97cc4.0 312201204.7accd a32@posting.goo gle.com...[color=blue]
        > 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.
        >[/color]
        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




        Comment

        • Bill Kelly

          #5
          Re: replacing two EOL chars by one


          "Xah Lee" <xah@xahlee.org > wrote:[color=blue]
          > 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.[/color]

          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.
          [color=blue]
          > 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. [...][/color]


          "Lighten up, Francis."


          Regards,

          Bill



          Comment

          • Rahul Jain

            #6
            Re: replacing two EOL chars by one

            Gunnar Hjalmarsson <noreply@gunnar .cc> writes:
            [color=blue]
            > Paul McGuire wrote:[color=green]
            >> I hope someone who knows the answer can respond, hopefully in more
            >> civil language.[/color]
            >
            > 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.[/color]

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

            --
            Rahul Jain
            rjain@nyct.net
            Professional Software Developer, Amateur Quantum Mechanicist

            Comment

            • Jürgen Exner

              #7
              Re: replacing two EOL chars by one

              Xah Lee wrote:[color=blue]
              > 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.[/color]
              [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


              Comment

              • Jay Tilton

                #8
                Re: replacing two EOL chars by one

                xah@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.

                Comment

                • Greg Menke

                  #9
                  Re: replacing two EOL chars by one


                  xah@xahlee.org (Xah Lee) writes:
                  [color=blue]
                  > 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:[/color]

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

                  Gregm

                  Comment

                  • Pascal Bourguignon

                    #10
                    Re: replacing two EOL chars by one

                    "Paul McGuire" <ptmcg@austin.r r.com> writes:
                    [color=blue]
                    > Is this language really necessary? I'm sure I've used these words myself,
                    > but they're certainly not appropriate in a public discussion.[/color]

                    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.

                    [color=blue]
                    > 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.[/color]

                    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_Bourgu ignon__ . * * . * .* .
                    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: postmaster@sco. com *************** ***

                    Comment

                    • Jesse Tov

                      #11
                      Re: replacing two EOL chars by one

                      Pascal Bourguignon <spam@thalassa. informatimago.c om>:[color=blue]
                      > Even with sed I find difficult to do that.[/color]

                      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

                      Comment

                      Working...