No, this is not what I am asking for. Is there a method to basically delete a blank line from a file using perl using the substitution 's/ / /' perl function.

Maybe using the m modifier?

Nope, not possible with that regex.

Bill's solution is the cleanest most elegant approach, but if you wish you could use a modified version of that as part of a larger script.

You should provide more details on what you're needing. Since you shot down Bill's solution, I can only assume that you need the solution to fit within your current script. Since you haven't shown us that script, we can't say with any confidence which one of the various approaches would suit your needs.

The OP said file in two different posts. Removing blank lines from a string is pretty easy. Doing it from a file is slightly more complicated because it more or less requires to read the file and write a new one (and doing the necessary house cleaning afterwards). There are a couple of way to do it transparently (for example with the -i command line option), but only because the necessary operations are done behind the scene.

Thats right, theres definitely some confusion. My assumption of the OP meaning string is based on the them wanting to use a regexp substitution, which would only be useful for this particular task once the file has been slurped into a string.

Even so, you have covered both, since your code would work with strings...

Code

my $IN_str = '

this

is

a

test

'; my $OUT_str = '';

open my $IN, '<', \$IN_str; open my $OUT, '>', \$OUT_str;

while (<$IN>) { # reading the file line by line next if /^\s*$/; # skipping blank lines (empty or only white spaces) print $OUT $_; # print other lines to output file }

Hi Bill, I do not really know what the OP really wants, but if, as I understand it, the OP wants to remove blank lines from a file, you haven't really done that. Your Perl one-liner is creating a new file with empty lines dropped, just as the (incomplete) code snippet I posted was doing. Now the OP says that she or he wants something else. I presume that she or he probably wants to modify the original file, not just create a new file. If such is the case, it is only a small modification (adding the -i option) to the one-liner you suggested, or possibly using another a one-liner based on my proposal:

Code

perl -i.bak -ne 'print unless /^\s*$/;' filename.txt

(Replacing single quote with double quotes under Windows or VMS.)

Now, since we don't understand the OP's real intents, we might be wasting our time and I do not intend to intervene anymore on this thread so long as these intents have not been clarified by the OP.

Excellent Job Bill! And btw Zhris I mentioned the m flag earlier. I apologize to everyone about the ambiguity of the post. It is truly important to specify either 'file' or 'string'. Thank you everyone for your suggestions.