Perl deleting a new line

I am unable to remove the newline from the output I get with the command on the linux server and have tried several different ways.

heres my code First try

$location = `curl -m 5 -sI 216.58.219.206 | grep "Location:"`;
chomp ($location);
print "before\n";
print "$location";
print "after\n";

      

doesn't work, it prints on a new line.

before
Location: http://www.google.com/
after

      

Second attempt

$location = `curl -m 5 -sI 216.58.219.206 | grep "Location:"`;
$location =~ s/\n//g;
print "before\n";
print "$location";
print "after\n";

      

The exit still doesn't work.

before
Location: http://www.google.com/
after

      

Raw Shell exit

[user@localhost dir]# curl -m 5 -sI 216.58.219.206 | grep "Location:"
Location: http://www.google.com/
[user@localhost dir]#

      

Doesn't work, I can still see a new line. Did I miss something?

+3


source to share


2 answers


Curl react returns with \ r \ n instead of \ n .

Use od (octal dump) to verify this ( -c means show characters):

curl -m 5 -sI 216.58.219.206 | grep Location: | od -c
0000000   L   o   c   a   t   i   o   n   :       h   t   t   p   :   /
0000020   /   w   w   w   .   g   o   o   g   l   e   .   c   o   m   /
0000040  \r  \n
0000042

      



You will need to remove both characters.

$location =~ s|\s*$||;  # Remove all trailing whitespace

      

+3


source


You can remove tab, spaces, newline and carriage return with tr



curl -m 5 -sI 216.58.219.206 | grep Location: | tr -d '\011\012\040' 

      

0


source







All Articles