簡體   English   中英

如何使用 Perl 將 Curl 輸出存儲到文件中?

[英]How can I store Curl output into a file using Perl?

我有一個目錄,里面有很多 .wav 文件。 我通過 curl 命令讀取每個文件的數據並將其存儲到不同的文件中。

這不是完整的腳本。 腳本的某些部分 -

@command= '--request POST --data-binary "@Raajpal_long.wav" 
 "https://xxxx.xx.xxxx:xxxx/SpeakerId
 action=search&confidence_threshold=0.0&number_of_matches=20&format=8K_PCM16"';

 $stdout=system("curl @command");

當我運行 perl 腳本時,它會在命令行窗口上提供輸出:

{"status": 0, "processing_time": 96.0, "enrollment_audio_time": 131.10000610351562, "matches": [{"speaker": "sw", "identification_score": 252.54136657714844}]}

我想將此輸出存儲到一個文件中。

我用了 -

open (FILE, ">1.txt") or die "Unable to open "1.txt";
$stdout=system("curl @command");
print FILE $stdout;

它只保存零(0);

誰能告訴我如何解決這個問題?

您已經開始使用 curl 來發出請求; 只使用 curl 的-o / --output選項來寫入文件而不是 stdout 會更干凈。

 -o, --output <file>

將輸出寫入而不是 stdout。 如果您使用 {} 或 [] 來獲取多個文檔,您可以在說明符中使用“#”后跟一個數字。 該變量將替換為正在獲取的 URL 的當前字符串。 像:

curl http://{one,two}.example.com -o "file_#1.txt"

或使用幾個變量,如:

curl http://{site,host}.host[1-5].com -o "#1_#2"

您可以根據您擁有的 URL 數量多次使用此選項。 例如,如果您在同一命令行上指定兩個 URL,則可以像這樣使用它:

curl -o aa example.com -o bb example.net

而且 -o 選項和 URLs 的順序無關緊要,只是第一個 -o 是第一個 URL 等等,所以上面的命令行也可以寫成

curl example.com example.net -o aa -o bb

您不能使用 system 來捕獲輸出,您可以使用反引號 `` 代替 system.

就像是:

my @command= '--request POST --data-binary "@Raajpal_long.wav"
 "https://services.govivace.com:49162/SpeakerId
action=search&confidence_threshold=0.0&number_of_matches=20&format=8K_PCM16"';

my $result = `curl @command`;

if ( $? == -1 ){
    print "\n Curl Command failed: $!\n";
} elsif ($? == 0 ) {
    print "$result\n";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM