如果您确实无法使用适当的JSON解析器(例如[1]) ,请尝试 基于的解决方案
:
jq
awk
Bash 4.x:
readarray -t values < <(awk -F" 'NF>=3 {print }' myfile.json)
Bash 3.x:
IFS=$'n' read -d '' -ra values < <(awk -F" 'NF>=3 {print }' myfile.json)
这会将所有属性 值 存储在Bash数组中
${values[@]},您可以使用进行检查
declare -p values。
这些解决方案具有局限性:
- 每个媒体资源必须位于各自的行中,
- 所有值都必须用双引号引起来,
- 不支持嵌入式转义的双引号。
所有这些限制加强了推荐使用正确的JSON解析器的建议。
注意:以下替代解决方案使用Bash 4.x +
readarray -t values命令,但它们也可以与Bash
3.x替代产品一起使用
IFS=$'n' read -d '' -ra values。
grep
+ cut
组合:单个
grep命令将不起作用(除非您使用 GNU-
grep见下文),但增加了
cut帮助:
readarray -t values < <(grep '"' myfile.json | cut -d '"' -f4)
GNUgrep
:
-P用于支持PCRE,PCRE支持
K删除到目前为止匹配的所有内容(一种替代后置断言的灵活方法)以及超前断言(
(?=...)):
readarray -t values < <(grep -Po ':s*"K.+(?="s*,?s*$)' myfile.json)
最后,这是一个 纯Bash(3.x +)解决方案 :
就性能而言,使之成为可行替代方案的是, 在每个循环迭代 中 无需调用外部工具 ; 但是,对于较大的输入文件,基于外部实用程序的解决方案将更快。
#!/usr/bin/env bashdeclare -a values # declare the array# Read each line and use regex parsing (with Bash's `=~` operator)# to extract the value.while read -r line; do # Extract the value from between the double quotes # and add it to the array. [[ $line =~ :[[:blank:]]+"(.*)" ]] && values+=( "${BASH_REMATCH[1]}" )done < myfile.jsondeclare -p values # print the array
[1]这是 基于健壮jq
的解决方案的外观(Bash 4.x):
readarray -t values < <(jq -r '.[]' myfile.json)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)