Arrays Bash将数组展平到键值对
我有一个下面提到的数组 阵列Arrays Bash将数组展平到键值对,arrays,linux,bash,shell,Arrays,Linux,Bash,Shell,我有一个下面提到的数组 阵列 wf.example.input1=/path/to/file1 wf.example.input2=/path/to/file2 wf.example.input3=["/path/to/file3","/path/to/file4"] declare-p数组给出以下输出 ([0]="wf.example.input1=/path/to/file1" [1]="wf.example.input2=/path/to/file2" [2]="wf.example
wf.example.input1=/path/to/file1
wf.example.input2=/path/to/file2
wf.example.input3=["/path/to/file3","/path/to/file4"]
declare-p数组
给出以下输出
([0]="wf.example.input1=/path/to/file1" [1]="wf.example.input2=/path/to/file2" [2]="wf.example.input3=[\"/path/to/file3\",\"/path/to/file4\"]")
我需要展平这个数组ib bash脚本,并给出如下输出
输出
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"
使用管道传输至
awk
的printf
获取格式:
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]
}
}'
使用管道传输至
awk
的printf
获取格式:
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]
}
}'
@anubhava这个阵列将作为我的输入。我不是创建此阵列的人。我需要像我的问题中提到的那样生成所需的输出。无论您得到什么输入,您都可以使用
declare-p array
@anubhava检查它。这个数组将作为我的输入。我不是创建此阵列的人。我需要生成问题中提到的所需输出。无论您得到什么输入,都可以使用declare-p array
检查它,这正是我所需要的+1这正是我所需要的+1.