Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.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_String_Shell_Unix_Awk - Fatal编程技术网

Arrays 在shell脚本中将逗号分隔的字符串拆分为数组值?

Arrays 在shell脚本中将逗号分隔的字符串拆分为数组值?,arrays,string,shell,unix,awk,Arrays,String,Shell,Unix,Awk,我的数据集(data.txt)如下所示[imageID、sessionID、height1、height2、x、y、crop]: 1,0c66824bfbba50ee715658c4e1aeacf6fda7e7ff,1296,4234,194,1536,0 2,0c66824bfbba50ee715658c4e1aeacf6fda7e7ff,1296,4234,194,1536,0 3,0c66824bfbba50ee715658c4e1aeacf6fda7e7ff,1296,4234,194,

我的数据集(data.txt)如下所示[imageID、sessionID、height1、height2、x、y、crop]:

1,0c66824bfbba50ee715658c4e1aeacf6fda7e7ff,1296,4234,194,1536,0
2,0c66824bfbba50ee715658c4e1aeacf6fda7e7ff,1296,4234,194,1536,0
3,0c66824bfbba50ee715658c4e1aeacf6fda7e7ff,1296,4234,194,1536,0
4,0c66824bfbba50ee715658c4e1aeacf6fda7e7ff,1296,4234,194,1536,950
这些是我希望使用的一组值。我不熟悉shell脚本:)我像这样逐行读取文件

cat $FILENAME | while read LINE
do
string=($LINE)
# PROCESSING THE STRING
done
现在,在上面的代码中,在获取字符串后,我希望执行以下操作: 1.将字符串拆分为逗号分隔的值。 2.将这些变量存储到数组中,如imageID[],sessionID[]

我需要访问这些值,以便使用imagemagick进行图像处理。 但是,我无法正确执行上述步骤

oIFS="$IFS"; IFS=',' 
set -A str $string
IFS="$oIFS"

echo "${str[0]}";
echo "${str[1]}";
echo "${str[2]}";
您可以像这样拆分和存储


查看有关Unix阵列的更多信息。

set-a对我不起作用(可能是因为OSX上较旧的BASH)

使用
read-a
发布替代解决方案,以防有人需要:

# init all your individual arrays here
imageId=(); sessionId=();

while IFS=, read -ra arr; do
    imageId+=(${arr[0]})
    sessionId+=(${arr[1]})
done < input.csv

# Print your arrays
echo "${imageId[@]}"
echo "${sessionId[@]}"
#在此处初始化所有单个数组
imageId=();sessionId=();
当IFS=,read-ra-arr;做
imageId+=(${arr[0]})
sessionId+=(${arr[1]})
完成
Ok,因此您可以拆分字符串并将其存储在数组中。如何将实体存储在不同的阵列中。我想创建imageID、sessionID等的数组。你能修改你的代码吗?非常感谢你的帮助<代码>设置-A对我不起作用(没有
-A
选项设置
)。您链接到的页面使用
声明
。简洁的解决方案涵盖了我的大部分需求!谢谢!当IFS=,read-ra-arr时,你不能使循环头
;执行
,然后去掉下一行?似乎初始化行
imageId=();sessionId=()也不是必需的。(虽然不会痛!)