How to delete newline with perl

input:

donkey

monkey


dance


drink




output should be:

donkey
monkey
dance
drink

try chomp

---------- Post updated at 09:49 AM ---------- Previous update was at 09:42 AM ----------

use strict;
use warnings;

open(FILE, "<", "a") or die;

my $data;
while ( $data = <FILE> ) {
    chomp($data);
    print $data, "\n" if ( $data =~ /./ );
}

close(FILE) or warn;

Any regular expression to solve this?

$ 
$ 
$ cat -n f24
     1    donkey
     2    
     3    monkey
     4    
     5    
     6    dance
     7    
     8    
     9    drink
$ 
$ 
$ perl -lne 'print if /./' f24
donkey
monkey
dance
drink
$ 
$ 

Please explain this line:

 $ perl -lne 'print if /./' f24

'print if / ./'

perl -i -pe 's/^\s+$//g' file
1 Like

. represents any character except a newline ( \n )

What does the dot character (".") represent in a regular expression?

tyler_durden

Ah... sorry, matrixmadhan - didn't notice your post. I wish the OP puts in just a wee bit of effort into understanding a script before dashing off with a counter post asking for an explanation.

I know this.But the normal if expression for perl is like this:

if(a<b){
    .................
    .................
}

Here it's 'print if /./'

---------- Post updated at 11:44 AM ---------- Previous update was at 11:42 AM ----------

Why is g option used for?
If i don't type g ,the result is same.

If i remove g option then it should be for the first case.Shouldn't it?