David
David

Reputation: 105

Remove empty lines and space with Perl

How do I remove empty lines and space from a string?

I did a search, but none of the solutions were able to fix my problem. When I print the string, I don't see any special character like \n \t \r. Here is my string:

$string= "Current configuration : 17448 bytes

                              |  Current configuration : 17331 bytes


 ";

I did the following:

$string =~ s/ ++/ /gs;

But the output came out like this:

Current configuration : 17448 bytes
 | Current configuration : 17331 bytes

And I want it to show like this:

Current configuration : 17448 bytes | Current configuration : 17331 bytes

Upvotes: 3

Views: 4572

Answers (2)

fugu
fugu

Reputation: 6568

my $string= "Current configuration : 17448 bytes

                              |  Current configuration : 17331 bytes


 ";

$string =~ s/\s+/ /gs;

print "$string\n"

Prints:

Current configuration : 17448 bytes | Current configuration : 17331 bytes 

Upvotes: 3

vmeln
vmeln

Reputation: 1309

 $string =~ s/\s+/ /gs;

Worked for me

Upvotes: 2

Related Questions