in reply to Remove whitespace between lines

My immediate thought is, “just use grep on the command line.”   You don’t need to write a custom program at all, if you don’t want to.   The regular-expression should identify the lines that you want to keep.

An “awk one-liner” is also useful in cases like these.   Again, no programming per se.

Within a program, there are many ways to do it ... some you’ve seen will “slurp” the entire file into memory, then manipulate it as a gigantic text-string.   However, I prefer to process line-at-a-time text files a line-at-a-time.

Replies are listed 'Best First'.
Re^2: Remove whitespace between lines
by Laurent_R (Canon) on Feb 03, 2015 at 22:36 UTC
    Hmm, sorry, but your answer is off-topic. The OP is writing a Perl script for achieving a given result, and your answer is: "Don't do it! Use grep!" But the code presented by the OP is not just trying to grep one file. It is looking for files in a directory, modifying these files and writing output in other files in another directory. It seems a bit difficult to do all this with a simple grep or even a awk one-liner. Of course, you could wrap the grep or the awk one-liner in a bash or sh or ksh script, but then the "no programming argument" is gone, you might as well do it in Perl, and it is likely to be more efficient in Perl.

    But there is an even more important factor: a Perl script, if well designed, is portable across different platforms. Even though the script shown by the OP has an (admittedly strange) "#! /usr/bin/perl -w" shebang line, the rest of the script seems to strongly indicate the OP is running her or his script under MS Windows, probably with Strawberry Perl. I pray you: how are you going to run a grep or a awk one-liner script under Windows (unless you are using Cygwin, but the OP does not seem to be doing that)? (Well, awk may probably have been ported under Windows, I do not know, but by far most Windows users don't have it.)

    Je suis Charlie.

      FYI, FWIW, several versions of awk have been ported to MS Windows. Until a few years ago, I used a version that was packaged along with make and a few other Unix tools as part of code generation application we use with many of our projects. We used the make and the awk from that package in our build scripts because everyone in the SW department had the package installed. Now we use Perl instead of awk (but still use make).

        Yes, thank you for the information, RonW. As I said in my previous post, I suspected that awk had been ported to Windows, but it is probably not commonly available (in the sense that it is not natively there on a fresh Windows install, so that most users probably don't have it). OTOH, the OP has Perl installed, and Perl is able to do just about everything that awk can do (and much more), so that recommending awk did not make much sense in that specific case.

        Je suis Charlie.