当前位置:网站首页>Awk processing JSON processing
Awk processing JSON processing
2022-07-07 21:15:00 【Chestnut less】
Nested parsing and lists will be a little bug... However, it meets the requirements of light use #!/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")
边栏推荐
- 寫一下跳錶
- 201215-03-19—cocos2dx内存管理–具体解释「建议收藏」
- Alibaba cloud award winning experience: how to mount NAS file system through ECS
- 神兵利器——敏感文件发现工具
- 私募基金在中國合法嗎?安全嗎?
- 华泰证券可以做到万一佣金吗,万一开户安全嘛
- Is private equity legal in China? Is it safe?
- 开户必须往账户里面赚钱吗,资金安全吗?
- Helix QAC 2020.2新版静态测试工具,最大限度扩展了标准合规性的覆盖范围
- Is embedded system really safe? [how does onespin comprehensively solve the IC integrity problem for the development team]
猜你喜欢
Static analysis of software defects codesonar 5.2 release
Measure the height of the building
AADL inspector fault tree safety analysis module
C语言多角度帮助你深入理解指针(1. 字符指针2. 数组指针和 指针数组 、数组传参和指针传参3. 函数指针4. 函数指针数组5. 指向函数指针数组的指针6. 回调函数)
Make this crmeb single merchant wechat mall system popular, so easy to use!
如何满足医疗设备对安全性和保密性的双重需求?
Cantata9.0 | new features
【C语言】指针进阶---指针你真的学懂了吗?
Helix QAC 2020.2 new static test tool maximizes the coverage of standard compliance
Is embedded system really safe? [how does onespin comprehensively solve the IC integrity problem for the development team]
随机推荐
SQL注入报错注入函数图文详解
Addition, deletion, modification and query of sqlhelper
恶魔奶爸 C
[function recursion] do you know all five classic examples of simple recursion?
Do you have to make money in the account to open an account? Is the fund safe?
刚开户的能买什么股票呢?炒股账户安全吗
私募基金在中国合法吗?安全吗?
部署、收回和删除解决方式—-STSADM和PowerShell「建议收藏」
UVA 11080 – Place the Guards(二分图判定)
I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!
恶魔奶爸 指南帖——简易版
The latest version of codesonar has improved functional security and supports Misra, c++ parsing and visualization
Klocwork code static analysis tool
AADL inspector fault tree safety analysis module
Unity3d 4.3.4f1执行项目
Jetty: configure connector [easy to understand]
Phoenix JDBC
[UVALive 6663 Count the Regions] (dfs + 离散化)[通俗易懂]
恶魔奶爸 A0 英文零基础的自我提升路
寫一下跳錶