in reply to how to use matching operator on newlines

Slurping can be a problem if your files are large. This one-liner avoids that by using -n instead of -p:

perl -i -ne '/^$/ or print' foo.txt

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: how to use matching operator on newlines
by sauoq (Abbot) on Jan 03, 2007 at 21:10 UTC
    This one-liner avoids that

    That may very well be what the OP actually wants, but it is not equivalent his stated goal.

    The OP's goal was to "translate double newlines to single newlines." Your code gets rid of blank lines. Consider what happens when a file starts with a blank line. Or, for another example, when there is a non-blank line followed by two blank lines. Etc.

    -sauoq
    "My two cents aren't worth a dime.";
Re^2: how to use matching operator on newlines
by ikegami (Patriarch) on Jan 03, 2007 at 21:02 UTC
    I don't think that accomplishes the goal. Your solution removes all the blank lines, not just the double-spacing (unlike the other solutions). For example, yours converts a\n\n\n\nb\n\n to a\nb\n rather than a\n\nb\n.

      I don't think that accomplishes the goal.

      It depends upon which of the stated goals you focus on. The op's description started with

      I have a unix-formatted double spaced file, so each line of text is followed by 2 linefeeds,...

      And if that is an accurate statement of the problem, my one-liner will work.

      If it's not accurate, or omits significant details, then it won't and a slightly more sophisticated one-liner is required:

      perl -e"BEGIN{$/=qq[\n\n]}" -ne"chop;print" junk.txt

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.