我的脚本为:
my $workItemCommentorURL = "https://almweb1.ipc.com/jts/users/sainis";
my $json_text= "curl -s -D - -k -b ~/.jazzcookies -o commentor.json -H \"Accept: application/x-oslc-cm*\" \"$workItemCommentorURL\"";
my $content = `cat commentor.json`;
my $WICommentCreator = `cat commentor.json | perl -l -ne '/<j.0:name>(.*)<\\/j.0:name>/ and print \$1'`;
print "NAME OF COMMENTOR ************-> \"$WICommentCreator\"\n";
这给出了输出:
NAME OF COMMENTOR ************-> "Shalini Saini
"
I.e Shalini Saini接着是一条新线。 而不是
NAME OF COMMENTOR ************-> "Shalini Saini"
为什么Saini之后会有新的一行,为什么报价会出现在下一行?我该怎么修剪它?
答案 0 :(得分:6)
简短回答:删除-l
开关,因为它会导致print
在结尾处换行。
答案很长:不要使用shell命令在Perl中运行Perl,这是非常多余的。只需正常阅读文件。
use strict;
use warnings;
my $workItemCommentorURL = "https://almweb1.ipc.com/jts/users/sainis";
my $json_text= "curl -s -D - -k -b ~/.jazzcookies -o commentor.json -H \"Accept: application/x-oslc-cm*\" \"$workItemCommentorURL\"";
my $content = do {
open my $fh, "<", "commentor.json" or die $!;
local $/; <$fh>;
};
my ($WICommentCreator) = $content =~ /<j.0:name>(.*)<\/j.0:name>/;
print "NAME OF COMMENTOR ************-> \"$WICommentCreator\"\n";