gpt4 book ai didi

linux - 数字数组之间有两个额外的空间

转载 作者:太空宇宙 更新时间:2023-11-04 10:09:25 27 4
gpt4 key购买 nike

我有两个这样的映射文件,如下所示:

primary-mapping.txt

{1=[343, 0, 686, 1372, 882, 196], 2=[687, 1, 1373, 883, 197, 736, 1030, 1569], 3=[1374, 2, 884, 737, 198, 1570], 4=[1375, 1032, 1424, 3, 885, 1228], 5=[1033, 1425, 4, 200, 886]}

secondary-mapping.txt

{1=[1152, 816, 1488, 336, 1008], 2=[1153, 0, 817, 337, 1489, 1009, 1297], 3=[1, 1154, 1490, 338], 4=[1155, 2, 339, 1491, 819, 1299, 1635], 5=[820, 1492, 340, 3, 1156]}

在上面的映射文件中,每个clientId都有primary和secondary的映射。例如:clientId 1343, 0, 686, 1372, 882, 196 主映射和 1152, 816, 1488, 336, 1008二次映射。对于其他 clientIds 也是如此。

下面是我的 shell 脚本,它在其中打印特定 clientid 的主要和次要映射:

#!/bin/bash
mapfiles=(primary-mapping.txt secondary-mapping.txt)

mappingsByClientID () {
(($# != 3)) && { echo "Insufficient arguments" >&2; exit 1; }
declare -n arr=$1
id=$2
file=${mapfiles[$3]}

if ! grep -q "[{ ]$id=" "$file"; then
echo "Couldn't find mapping for id '$id' in file '$file'" >&2
exit 1
fi
mapfile -t arr < <(sed -r "s/.*[{ ]$id=\[([^]\]+).*/\1/" "$file" | tr -s , '\n')
if ((${#arr[@]} == 0)); then
echo "Couldn't find mapping for id '$id' in file '$file'" >&2
exit 1
fi
}

mappingsByClientID pri 3 0
mappingsByClientID sec 3 1

echo "primary files: ${pri[@]}"
echo "secondary files: ${sec[@]}"

现在,当我打印出 prisec 数组时,我看到数组中每个元素之间有一个额外的空间。这意味着每个元素之间有两个空格。我不确定这里出了什么问题。在我看来,我相信 sed 行有问题。例如,主文件数组是这样打印出来的:

1220 1374 415 1097 1219 557 401

最佳答案

tr 调用中去除空格和逗号:

mapfile -t arr < <(sed -r "s/.*[{ ]$id=\[([^]\]+).*/\1/" "$file" | tr -s '[ ,]' '\n')

关于linux - 数字数组之间有两个额外的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49991888/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com