我有一个配置文件,如下所示
"gpu_thread_num" : 6,
"gpu_threads_conf" : [
{ "index" : 0, "intensity" : 1000, "worksize" : 8, "affine_to_cpu" : false },
{ "index" : 1, "intensity" : 1000, "worksize" : 8, "affine_to_cpu" : false },
{ "index" : 2, "intensity" : 1000, "worksize" : 8, "affine_to_cpu" : false },
{ "index" : 3, "intensity" : 1000, "worksize" : 8, "affine_to_cpu" : false },
{ "index" : 4, "intensity" : 1000, "worksize" : 8, "affine_to_cpu" : false },
{ "index" : 5, "intensity" : 1000, "worksize" : 8, "affine_to_cpu" : false },
],"gpu_threads_conf“中的行数必须始终等于"gpu_thread_num”,这个数目会随连接的GPU数而变化。我有一个start.sh脚本来检查这个数字,并用sed更改"gpu_thread_num“,但我不知道如何更改"gpu_threads_conf”。
我试着自己找出怎么做的希望破灭了,真的需要你的帮助。
发布于 2017-08-26 08:20:17
如果您想根据行数更新"gpu_thread_num“的数量,可以使用以下脚本:
#!/bin/sh
#Take the first line
idx=$(grep -n gpu_threads_conf "$1")
#line after gpu_thread...
START=$(($(echo "$idx" | sed "s|:.*||")))
idx2=$(grep -n "]," "$1")
END=$(($(echo "$idx2" | sed "s|:.*||") - 1))
#Returns 3
echo $START
#returns 9
echo $END
#returns 6
RESULT=$(($END - $START))
#substitute the number in the file
sed -i "s|\"gpu_thread_num\" :.*|\"gpu_thread_num\" : $RESULT,|" $1它将直接更新您的配置文件...
sh Test.sh MyConf.cfg我希望通过这样或那样的解决方案,你会找到你的幸福:)
发布于 2017-08-26 07:34:11
我不确定是否理解得很好,但您可以尝试这样做:
#!/bin/sh
echo "\"gpu_threads_conf\" : [ " > TOTO.txt
END=6
for i in $(seq 2 $END); do
echo "$i"
echo "{ \"index\" : $i, \"intensity\" : 1000, \"worksize\" : 8, \"affine_to_cpu\" : false }," >> TOTO.txt
done
echo "]," >> TOTO.txt结果:
$ cat TOTO.txt
"gpu_threads_conf" : [
{ index : 1, intensity : 1000, worksize : 8, affine_to_cpu : false },
{ index : 2, intensity : 1000, worksize : 8, affine_to_cpu : false },
{ index : 3, intensity : 1000, worksize : 8, affine_to_cpu : false },
{ index : 4, intensity : 1000, worksize : 8, affine_to_cpu : false },
{ index : 5, intensity : 1000, worksize : 8, affine_to_cpu : false },
{ index : 6, intensity : 1000, worksize : 8, affine_to_cpu : false },
],只需使用带有"gpu_thread_num“编号的END,我想就是这样。
我希望我很好地理解了这个问题。
致敬托马斯
https://stackoverflow.com/questions/45890340
复制相似问题