[英]Perl Removing New Line
I cant seem to remove the new line from an output I get by executing a command on linux server and have tried several different ways 我似乎无法通过在linux服务器上执行命令从输出中删除新行,并尝试了几种不同的方法
heres my 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, it gets printed with a 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, I still see a new line. 也不工作,我仍然看到一个新的线。 Am I missing something?
我错过了什么吗?
The curl response is coming back with \\r\\n instead of just \\n . 卷曲响应将以\\ r \\ n而不仅仅是\\ n返回 。
Use od (octal-dump) to see for yourself ( -c means show characters): 使用od (八进制转储)自己查看( -c表示显示字符):
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
您可以使用tr删除制表符,空格,换行符和回车符
curl -m 5 -sI 216.58.219.206 | grep Location: | tr -d '\011\012\040'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.