我有一個問題。我想編寫一個perl腳本來將Mailgun輸出解析爲csv格式。我會假設'拆分'和'連接'功能可以適用於此過程。下面是一些示例數據:mailgun報告爲csv格式perl
樣本數據
{
"geolocation": {
"city": "Random City",
"region": "State",
"country": "US"
},
"url": "https://www4.website.com/register/1234567",
"timestamp": "1237854980723.0239847"
}
{
"geolocation": {
"city": "Random City2",
"region": "State2",
"country": "mEXICO"
},
"url": "https://www4.website2.com/register/ABCDE567",
"timestamp": "1237854980723.0239847"
}
所需的輸出
「城市」, 「區域」, 「國家」, 「URL」, 「時間戳」
「隨機城市」,「州」,「美國」,「https://www4.website.com/register/1234567」,「1237854980723.0239847」
「隨機City_2」,「State_2」,「mEXICO」,「www4.website2.com/ABCDE567","1234.jpg」,網址爲:http://www4.website2.com/ABCDE567 ,,「1237854980723.0239847_2」
我的目標是將我的Sample數據創建爲逗號分隔的CSV文件。我不確定如何去解決這個問題。通常我會嘗試通過批處理文件中的一系列單行程來破解,但我更喜歡perl腳本。真實的數據將包含更多信息。但是,只要弄清楚如何解析一般結構就沒問題。
這是我在一個批處理文件中。
代碼
perl -p -i.bak -e "s/(,$|,+ +$|^.*?{$|^.*?}.*?$|^.*?],.*?$)//gi" file.txt
rem Removes all unnecessary characters and lines with { and }.^
perl -p -i.bak -e "s/(^ +| +$)//gi" file.txt
perl -p -i.bak -e "s/^\n$//gi" file.txt
rem Removes all blank lines in initial file. Next one-liner takes care of trailing and beginning
rem whitespace. The file is nice and clean now.
perl -p -e "s/(^\".*?\"):.*?$/$1/gi" file.txt > header.txt
rem retains only header info and puts into 'header.txt'^
perl -p -e "s/^\".*?\": +(\".*?\"$)/$1/gi" file.txt > data.txt
rem retains only data that is associated with each field.
perl -p -i.bak -e "s/\n/,/gi" data.txt
rem replaces new line character with ',' delimiter.
perl -p -i.bak -e "s/^/\n/gi" data.txt
rem drops data down a line
perl -p -i.bak -e "s/\n/,/gi" header.txt
rem replaces new line character with ',' delimiter.
copy header.txt+data.txt report.txt
rem copies both files together. Since there is the same amount of fields as there are data
rem delimiters, the columns and headers match.
我的輸出
「城市」, 「區域」, 「國家」, 「URL」, 「時間戳」
「隨機城」 「國家」,「美國」,「https://www4.website.com/register/1234567」,1237854980723.0239847
這是做的伎倆,但濃縮腳本會更好。變化的情況會影響到這個批處理腳本,我需要更堅實的東西。有什麼建議麼??
使用[JSON](https://metacpan.org/pod/JSON)。 – jm666 2014-08-27 21:38:48