Reputation: 897
I have an array mentioned below.
Array
wf.example.input1=/path/to/file1
wf.example.input2=/path/to/file2
wf.example.input3=["/path/to/file3","/path/to/file4"]
declare -p Array
gives me below output.
([0]="wf.example.input1=/path/to/file1" [1]="wf.example.input2=/path/to/file2" [2]="wf.example.input3=[\"/path/to/file3\",\"/path/to/file4\"]")
I need to flatten this array ib bash script and give me output like below.
Output
name:"wf.example.input1", value:"/path/to/file1"
name:"wf.example.input2", value:"/path/to/file2"
name:"wf.example.input3", value:"/path/to/file3"
name:"wf.example.input3", value:"/path/to/file4"
Upvotes: 0
Views: 764
Reputation: 785701
Using printf
piped to awk
to get formatting:
declare -a arr='([0]="wf.example.input1=/path/to/file1"
[1]="wf.example.input2=/path/to/file2"
[2]="wf.example.input3=[\"/path/to/file3\",\"/path/to/file4\"]")'
printf "%s\n" "${arr[@]}" |
awk -F= '{
n=split($2, a, /,/)
for (i=1; i<=n; i++) {
gsub(/^[^"]*"|"[^"]*$/, "", a[i])
printf "name:\"%s\", value:\"%s\"\n", $1, a[i]
}
}'
Output:
name:"wf.example.input1", value:"/path/to/file1"
name:"wf.example.input2", value:"/path/to/file2"
name:"wf.example.input3", value:"/path/to/file3"
name:"wf.example.input3", value:"/path/to/file4"
Upvotes: 3