擷取資料跟資料轉換 - Linux

By Caitlin
at 2016-10-24T19:36
at 2016-10-24T19:36
Table of Contents
※ 引述《asdrt (安靜)》之銘言:
: {
: "id":"674579ca-8ae2-4f3c-8c2b-c8a28e106f75",
: "macAddr":"04000207",
: "data":"41303132352d3039302d31",
: "buff":"2016-10-24T04:15:58.133Z",
: "recv":"2016-10-24T04:15:57.000Z",
: "extra":{"gwip":"172.16.5.15",
: "gwid":"00001c497b3b8127",
: "repeater":"00000000ffffffff",
: "systype":4,
: "rssi":-123.2,
: "snr":-12.2}
: }
: 收到的資料並沒有分行,想說能不能寫個 shell 讓他自動分行
: 目前只是把收到的資料利用 >> 寫入到 txt 裡面
: 想說如果可以自動分行
: 這樣在資料擷取上也比較簡單
分成多行是可以,但是你後續用 >> 寫入附加到時候整個混在一起,這樣可以解析嗎?
單純分成多行很簡單,一般系統都有裝 python 這個語言的直譯器,那這樣使用:
$ cat xxxx.json | python -m json.tool 即可。
像是:
$ cat file.json
{"current_user_url":"https://api.github.com/user","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}"}
$ cat file.json | python -m json.tool
{
"current_user_url": "https://api.github.com/user",
"starred_gists_url": "https://api.github.com/gists/starred",
"starred_url": "https://api.github.com/user/starred{/owner}{/repo}",
"team_url": "https://api.github.com/teams",
"user_url": "https://api.github.com/users/{user}"
}
都幫你排版排好了,也是多行輸出。不過你的需求都多行放成一個檔案了,解析上是另外一個層級問題了。
: 所以來請教說是否可以透過 , 來做為每一行的分行代表跟執行方式
: 另外想請問的是
: 中間"41303132352d3039302d31" 這一串是ascii code 16進位
: 有沒有工具可以將 這一串轉換回 string
: 以上
: 感謝
: OS :ubuntu 10.04 LTS
可以執行 python 命令進入互動模式,輸入 "41303132352d3039302d31".decode('hex')
就可以得到:
A0125-090-1
最後推薦學一點程式語言有助於問題解決,python 這個 script 很好用可以解決很多需求。
--
: {
: "id":"674579ca-8ae2-4f3c-8c2b-c8a28e106f75",
: "macAddr":"04000207",
: "data":"41303132352d3039302d31",
: "buff":"2016-10-24T04:15:58.133Z",
: "recv":"2016-10-24T04:15:57.000Z",
: "extra":{"gwip":"172.16.5.15",
: "gwid":"00001c497b3b8127",
: "repeater":"00000000ffffffff",
: "systype":4,
: "rssi":-123.2,
: "snr":-12.2}
: }
: 收到的資料並沒有分行,想說能不能寫個 shell 讓他自動分行
: 目前只是把收到的資料利用 >> 寫入到 txt 裡面
: 想說如果可以自動分行
: 這樣在資料擷取上也比較簡單
分成多行是可以,但是你後續用 >> 寫入附加到時候整個混在一起,這樣可以解析嗎?
單純分成多行很簡單,一般系統都有裝 python 這個語言的直譯器,那這樣使用:
$ cat xxxx.json | python -m json.tool 即可。
像是:
$ cat file.json
{"current_user_url":"https://api.github.com/user","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}"}
$ cat file.json | python -m json.tool
{
"current_user_url": "https://api.github.com/user",
"starred_gists_url": "https://api.github.com/gists/starred",
"starred_url": "https://api.github.com/user/starred{/owner}{/repo}",
"team_url": "https://api.github.com/teams",
"user_url": "https://api.github.com/users/{user}"
}
都幫你排版排好了,也是多行輸出。不過你的需求都多行放成一個檔案了,解析上是另外一個層級問題了。
: 所以來請教說是否可以透過 , 來做為每一行的分行代表跟執行方式
: 另外想請問的是
: 中間"41303132352d3039302d31" 這一串是ascii code 16進位
: 有沒有工具可以將 這一串轉換回 string
: 以上
: 感謝
: OS :ubuntu 10.04 LTS
可以執行 python 命令進入互動模式,輸入 "41303132352d3039302d31".decode('hex')
就可以得到:
A0125-090-1
最後推薦學一點程式語言有助於問題解決,python 這個 script 很好用可以解決很多需求。
--
Tags:
Linux
All Comments

By Mary
at 2016-10-26T13:03
at 2016-10-26T13:03

By Elvira
at 2016-10-30T10:42
at 2016-10-30T10:42
Related Posts
租用網站主機及網址

By Kyle
at 2016-10-24T14:40
at 2016-10-24T14:40
nginx的rewrite問題

By Zenobia
at 2016-10-24T12:31
at 2016-10-24T12:31
擷取資料跟資料轉換

By Kelly
at 2016-10-24T12:22
at 2016-10-24T12:22
ssl設定的網站導向

By Wallis
at 2016-10-24T10:26
at 2016-10-24T10:26
Raspberry Pi, VNC 心得

By Ursula
at 2016-10-23T15:18
at 2016-10-23T15:18