Text-Processing
文本文件轉 JSON
我有一個文本文件
IP:PORT
,例如1.1.1.1:1919 2.2.2.2:111 1.1.1.1:987
我需要在具有 JSON 格式的腳本中使用它們:
async def main(loop): servers = [{ "address": "ip", "port": port }, { "address": "ip", "port": port }]
我需要輸出為
async def main(loop): servers = [{ "address": "1.1.1.1", "port": 1919 }, { "address": "2.2.2.2, "port": 111 }, { "address": "1.1.1.1, "port": 987 }]
我正在使用 Linux。
jq -nRr ' [ inputs | split(":") | {address: first, port: last} ] | "async def main(loop):\n servers = \(.)" ' addresses
輸出
async def main(loop): servers = [{"address":"1.1.1.1","port":"1919"},{"address":"2.2.2.2","port":"111"},{"address":"1.1.1.1","port":"987"}]