If you really cannot use a proper JSON parser such as jq
[1]
, try an awk
-based solution:
Bash 4.x:
readarray -t values < <(awk -F" 'NF>=3 {print $4}' myfile.json)
Bash 3.x:
IFS=$'
' read -d '' -ra values < <(awk -F" 'NF>=3 {print $4}' myfile.json)
This stores all property values in Bash array ${values[@]}
, which you can inspect with
declare -p values
.
These solutions have limitations:
- each property must be on its own line,
- all values must be double-quoted,
- embedded escaped double quotes are not supported.
All these limitations reinforce the recommendation to use a proper JSON parser.
Note: The following alternative solutions use the Bash 4.x+ readarray -t values
command, but they also work with the Bash 3.x alternative, IFS=$'
' read -d '' -ra values
.
grep
+ cut
combination: A single grep
command won't do (unless you use GNU grep
- see below), but adding cut
helps:
readarray -t values < <(grep '"' myfile.json | cut -d '"' -f4)
GNU grep
: Using -P
to support PCREs, which support K
to drop everything matched so far (a more flexible alternative to a look-behind assertion) as well as look-ahead assertions ((?=...)
):
readarray -t values < <(grep -Po ':s*"K.+(?="s*,?s*$)' myfile.json)
Finally, here's a pure Bash (3.x+) solution:
What makes this a viable alternative in terms of performance is that no external utilities are called in each loop iteration; however, for larger input files, a solution based on external utilities will be much faster.
#!/usr/bin/env bash
declare -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.json
declare -p values # print the array
[1] Here's what a robust jq
-based solution would look like (Bash 4.x):
readarray -t values < <(jq -r '.[]' myfile.json)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…