blank line deletion in a file using perl

Dear All,

I am looking for an option in perl using which i could delete empty lines in a file.

Or

the alternative of sed '/^$/d' <filename> in perl. Sed is not working in my perl script :frowning:

Pls help me out .

Thanks,
VG

$ cat input
A|3k|sdgs|211

C|dd|
D||gfgsfg|ff

22|fdfdfsdfsdf
$ perl -ne 'print unless(/^\s*$/);' input
A|3k|sdgs|211
C|dd|
D||gfgsfg|ff
22|fdfdfsdfsdf

Hi Rikxik,

thanks for the reply.

But it's not working for me. let me elaborate:

my script has to get the last line in a file to a perl variable as a string. Now, the file has last line as empty one. Hence i can't directly put it to my string variable as i have to get the last non empty line into the variable.

e.g.,

cat input.txt
Hi there
<blank line>

my variable has to get the last line as:

$myvar=`tail -1 input.txt`;

The problem is that last line is empty and my requirement is the string which is last line in a file and is non empty.

Regards,
Vinod.

Well its not expected to work since you initially asked for "I am looking for an option in perl using which i could delete empty lines in a file." and now "i have to get the last non empty line into the variable". Make up your mind before you ask. This should work:

perl -e 'while(<>) {$var=$_ unless(/^\s*$/)}; print $var;' input

Thanks for your help rikxik.