当前位置:网站首页>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")
边栏推荐
- Static analysis of software defects codesonar 5.2 release
- 万字总结数据存储,三大知识点
- Unity3d 4.3.4f1 execution project
- [uvalive 6663 count the regions] (DFS + discretization) [easy to understand]
- 使用高斯Redis实现二级索引
- 国家正规的股票交易app有哪些?使用安不安全
- POJ 3140 contents division "suggestions collection"
- Phoenix JDBC
- C language helps you understand pointers from multiple perspectives (1. Character pointers 2. Array pointers and pointer arrays, array parameter passing and pointer parameter passing 3. Function point
- Helix QAC 2020.2 new static test tool maximizes the coverage of standard compliance
猜你喜欢
Small guide for rapid formation of manipulator (11): standard nomenclature of coordinate system
Tensorflow2.x下如何运行1.x的代码
OneSpin | 解决IC设计中的硬件木马和安全信任问题
C language helps you understand pointers from multiple perspectives (1. Character pointers 2. Array pointers and pointer arrays, array parameter passing and pointer parameter passing 3. Function point
恶魔奶爸 B3 少量泛读,完成两万词汇量+
Intelligent software analysis platform embold
Cantata9.0 | 全 新 功 能
如何满足医疗设备对安全性和保密性的双重需求?
Is embedded system really safe? [how does onespin comprehensively solve the IC integrity problem for the development team]
SQL注入报错注入函数图文详解
随机推荐
CodeSonar如何帮助无人机查找软件缺陷?
恶魔奶爸 B2 突破语法,完成正统口语练习
Is it safe to open an account of BOC shares in kainiu in 2022?
Klocwork 代码静态分析工具
死锁的产生条件和预防处理[通俗易懂]
OneSpin | 解决IC设计中的硬件木马和安全信任问题
现在网上开户安全么?想知道我现在在南宁,到哪里开户比较好?
HDU4876ZCC loves cards(多校题)
Codeforces round 296 (Div. 2) A. playing with paper[easy to understand]
Jetty: configure connector [easy to understand]
Do you have to make money in the account to open an account? Is the fund safe?
OpenGL super classic learning notes (1) the first triangle "suggestions collection"
UVA 11080 – Place the Guards(二分图判定)
How to meet the dual needs of security and confidentiality of medical devices?
Klocwork code static analysis tool
Is it safe to open an account online now? I want to know where I can open an account in Nanning now?
Differences and connections between MinGW, mingw-w64, tdm-gcc and other tool chains "suggestions collection"
Écrivez une liste de sauts
开户还得用身份证银行卡安全吗,我是小白不懂
UVA 12230 – crossing rivers (probability) "suggested collection"