擷取資料跟資料轉換 - Linux

Caitlin avatar
By Caitlin
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 很好用可以解決很多需求。


--
Tags: Linux

All Comments

Mary avatar
By Mary
at 2016-10-26T13:03
真是好人!!
Elvira avatar
By Elvira
at 2016-10-30T10:42

租用網站主機及網址

Kyle avatar
By Kyle
at 2016-10-24T14:40
想請問一下 如果要架設網站的話 有推薦的網路主機及購買TW網址的服務商嗎 TW網址只能跟國內的服務商購買嗎 - ...

nginx的rewrite問題

Zenobia avatar
By Zenobia
at 2016-10-24T12:31
各位好 我正在練習怎麼使用nginx 我現在遇到一個問題 不知道怎麼解決 懇請大大幫忙 問題: 網址: http://tw.kimo.com/aa/bb/cc.html 我希望讓使用者可以不用填寫html 所以我寫了如下 location / { try_files $uri$arg ...

擷取資料跟資料轉換

Kelly avatar
By Kelly
at 2016-10-24T12:22
{ and#34;idand#34;:and#34;674579ca-8ae2-4f3c-8c2b-c8a28e106f75and#34;, and#34;macAddrand#34;:and#34;04000207and#34;, and#34;dataand#34;:and#34;41303132352 ...

ssl設定的網站導向

Wallis avatar
By Wallis
at 2016-10-24T10:26
大家好,之前有在此版問過ssl設定,經過一週多後,還是設定失敗啊(崩潰 先說環境和設定: 1. NameVirtualHost有兩組 2. andlt;VirtualHostandgt;(略)andlt;/VirtualHostandgt;一堆,有些用第一組NameVirtualHost,有些 用第二組 ...

Raspberry Pi, VNC 心得

Ursula avatar
By Ursula
at 2016-10-23T15:18
安裝新買的Raspberry Pi 3,突然覺得老是用ssh真的很宅 潮男都是開VNC再用Terminal(好像和直接ssh沒差)! 研究了VNC的幾個模式,也順便窺探自由軟體的商業模式,Open Source是要怎麼賺錢。 文章請看底下note http://lihgong.blogspot.tw/20 ...