当前位置:网站首页>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")
边栏推荐
- 95年专注安全这一件事 沃尔沃未来聚焦智能驾驶与电气化领域安全
- A brief understanding of the in arc__ bridge、__ bridge_ Retained and__ bridge_ transfer
- OneSpin | 解决IC设计中的硬件木马和安全信任问题
- 如何满足医疗设备对安全性和保密性的双重需求?
- Jetty: configure connector [easy to understand]
- Flask1.1.4 werkzeug1.0.1 source code analysis: Routing
- 国家正规的股票交易app有哪些?使用安不安全
- Jetty:配置连接器[通俗易懂]
- Data sorting in string
- 如何挑选基金产品?2022年7月份适合买什么基金?
猜你喜欢
Helix QAC 2020.2 new static test tool maximizes the coverage of standard compliance
Details of C language integer and floating-point data storage in memory (including details of original code, inverse code, complement, size end storage, etc.)
【OpenCV 例程200篇】223. 特征提取之多边形拟合(cv.approxPolyDP)
程序猿赚的那点钱算个P啊!
Klocwork code static analysis tool
95年专注安全这一件事 沃尔沃未来聚焦智能驾驶与电气化领域安全
Lex & yacc of Pisa proxy SQL parsing
OneSpin | 解决IC设计中的硬件木马和安全信任问题
软件缺陷静态分析 CodeSonar 5.2 新版发布
Helix QAC 2020.2新版静态测试工具,最大限度扩展了标准合规性的覆盖范围
随机推荐
Data sorting in string
智能软件分析平台Embold
95年专注安全这一件事 沃尔沃未来聚焦智能驾驶与电气化领域安全
easyui 日期控件清空值
浅解ARC中的 __bridge、__bridge_retained和__bridge_transfer
Validutil, "Rethinking the setting of semi supervised learning on graphs"
智能交通焕发勃勃生机,未来会呈现哪些巨变?[通俗易懂]
如何满足医疗设备对安全性和保密性的双重需求?
刚开户的能买什么股票呢?炒股账户安全吗
如何满足医疗设备对安全性和保密性的双重需求?
华泰证券可以做到万一佣金吗,万一开户安全嘛
单词反转实现「建议收藏」
DataTable数据转换为实体
2022年在启牛开中银股票的账户安全吗?
国家正规的股票交易app有哪些?使用安不安全
[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System
开户还得用身份证银行卡安全吗,我是小白不懂
写一下跳表
[uvalive 6663 count the regions] (DFS + discretization) [easy to understand]
恶魔奶爸 A3阶段 近常速语流初接触