regex - Perl Removing New Line -


i cant seem remove new line output executing command on linux server , have tried several different ways

heres code 1st try

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

output, doesnt work, gets printed new line.

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

2nd try

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

output, still doesn't work.

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

raw shell output

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

doesnt work either, still see new line. missing something?

the curl response coming \r\n instead of \n.

use od (octal-dump) see (-c means show characters):

curl -m 5 -si 216.58.219.206 | grep location: | od -c 0000000   l   o   c     t     o   n   :       h   t   t   p   :   / 0000020   /   w   w   w   .   g   o   o   g   l   e   .   c   o   m   / 0000040  \r  \n 0000042 

you need remove both characters.

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

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -