Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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 用于创建数组的Shell脚本_Arrays_Shell_Grep - Fatal编程技术网

Arrays 用于创建数组的Shell脚本

Arrays 用于创建数组的Shell脚本,arrays,shell,grep,Arrays,Shell,Grep,我希望对unix文件中的每个数字运行第二个文件中包含确切内容的grep,并将结果放入数组中 第一个文件字的格式如下: a b c d 第二个是频率 f 3 b 8 d 4 v 2 a 5 c 2 因此,我想创建包含以下内容的数组: [a 5] [b 8] [c 2] [d 4] 非常感谢您的帮助 您需要重置用于阵列的IFS变量delimeter OIFS=$IFS #save original IFS=',' arr=($(awk 'FNR==NR {a[$1]=$2;next} a[$1

我希望对unix文件中的每个数字运行第二个文件中包含确切内容的grep,并将结果放入数组中

第一个文件字的格式如下:

a
b
c
d
第二个是频率

f 3
b 8
d 4
v 2
a 5
c 2
因此,我想创建包含以下内容的数组:

[a 5]
[b 8]
[c 2]
[d 4]

非常感谢您的帮助

您需要重置用于阵列的IFS变量delimeter

OIFS=$IFS #save original
IFS=','
arr=($(awk 'FNR==NR {a[$1]=$2;next} a[$1] {print $1,a[$1]","}' second first))
IFS=$OIFS
使用join的解决方案可以是:

sort second > second.sort
IFS=','
arr=($(join first second.sort | tr '\n' ','))
rm second.sort
输出:

echo ${arr[0]}
a 5
echo ${arr[0]}
a 5