'How to extract multiple substrings surrounded by double-quotes from a longer string
I am trying to process the output of another script that looks a little something like this:
xxx "ABCD" xxx xxx ["EFGH","IJKL","MNOP","QRST","UVWX","YZ12"]
What I want to do is to be able to find the first substring surrounded by quotes, confirm the value (i.e. "ABCD") and then take all the remaining substrings (there is a variable number of substrings) and put them in an array.
I've been looking around for the answer to this but the references I've been able to find involve just extracting one substring and not multiples.
Solution 1:[1]
This awk tests for the content between the first pair of " characters, and extracts everything between subsequent pairs.
awk -v q="ABCD" -F'"' '$2==q{for (i=4; i<=NF; i+=2) print $i}'
To populate a bash array, you could use mapfile and process substitution:
mapfile -t arr < <( … )
Testing:
mapfile -t arr < <(
awk -v q="ABCD" -F'"' '$2==q{for (i=4; i<=NF; i+=2) print $i}' \
<<< 'xxx "ABCD" xxx xxx ["EFGH","IJKL","MNOP","QRST","UVWX","YZ12"]'
)
printf '%s\n' "${arr[@]}"
EFGH
IJKL
MNOP
QRST
UVWX
YZ12
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | pmf |
