Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Bash将数组展平到键值对_Arrays_Linux_Bash_Shell - Fatal编程技术网

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.