当前位置:网站首页>awk处理JSON处理
awk处理JSON处理
2022-07-07 18:37:00 【栗少】
嵌套解析和列表会有点 bug... 不过满足轻度使用了 #!/bin/bash json_str='{ "access_token":"12345678-1234-1234-1234-123456789012", "token_type":"bearer", "refresh_token":"12345678-1234-1234-1234-12345678901" }' get_json_value() { awk -v json="$1" -v key="$2" -v defaultValue="$3" 'BEGIN{ foundKeyCount = 0 while (length(json) > 0) { pos = match(json, "\""key"\"[ \\t]*?:[ \\t]*"); if (pos == 0) {if (foundKeyCount == 0) {print defaultValue;} exit 0;} ++foundKeyCount; start = 0; stop = 0; layer = 0; for (i = pos + length(key) + 1; i <= length(json); ++i) { lastChar = substr(json, i - 1, 1) currChar = substr(json, i, 1) if (start <= 0) { if (lastChar == ":") { start = currChar == " " ? i + 1: i; if (currChar == "{" || currChar == "[") { layer = 1; } } } else { if (currChar == "{" || currChar == "[") { ++layer; } if (currChar == "}" || currChar == "]") { --layer; } if ((currChar == "," || currChar == "}" || currChar == "]") && layer <= 0) { stop = currChar == "," ? i : i + 1 + layer; break; } } } if (start <= 0 || stop <= 0 || start > length(json) || stop > length(json) || start >= stop) { if (foundKeyCount == 0) {print defaultValue;} exit 0; } else { print substr(json, start, stop - start); } json = substr(json, stop + 1, length(json) - stop) } }' } echo $(get_json_value "${json_str}" "refresh_token")
边栏推荐
- Micro service remote debug, nocalhost + rainbow micro service development second bullet
- Spark 判断DF为空
- 深度学习模型压缩与加速技术(七):混合方式
- Measure the height of the building
- With st7008, the Bluetooth test is completely grasped
- Useful win11 tips
- 取两个集合的交集
- 论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》
- 凌云出海记 | 赛盒&华为云:共助跨境电商行业可持续发展
- Prometheus remote_write InfluxDB,unable to parse authentication credentials,authorization failed
猜你喜欢
Nebula Importer 数据导入实践
AADL Inspector 故障树安全分析模块
Small guide for rapid formation of manipulator (11): standard nomenclature of coordinate system
Machine learning notes - explore object detection datasets using streamlit
如何满足医疗设备对安全性和保密性的双重需求?
如何满足医疗设备对安全性和保密性的双重需求?
School 1 of vulnhub
最新版本的CodeSonar改进了功能安全性,支持MISRA,C ++解析和可视化
【C语言】指针进阶---指针你真的学懂了吗?
使用camunda做工作流设计,驳回操作
随机推荐
CodeSonar如何帮助无人机查找软件缺陷?
AIRIOT助力城市管廊工程,智慧物联守护城市生命线
Deep learning model compression and acceleration technology (VII): mixed mode
Small guide for rapid formation of manipulator (11): standard nomenclature of coordinate system
Spark judges that DF is empty
ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
Small guide for rapid formation of manipulator (12): inverse kinematics analysis
Precautions for cjson memory leakage
Alibaba cloud award winning experience: how to mount NAS file system through ECS
Write a jump table
How does codesonar help UAVs find software defects?
Try the tuiroom of Tencent cloud (there is an appointment in the evening, which will be continued...)
[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System
机械臂速成小指南(十二):逆运动学分析
让这个CRMEB单商户微信商城系统火起来,太好用了!
Splicing and splitting of integer ints
About cv2 dnn. Readnetfromonnx (path) reports error during processing node with 3 inputs and 1 outputs [exclusive release]
使用 BR 备份 TiDB 集群数据到 Azure Blob Storage
Dachang classic pointer written test questions
Validutil, "Rethinking the setting of semi supervised learning on graphs"