0

I have a config file that looking like this

"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 },
],

The number of lines in "gpu_threads_conf" always must be equal to "gpu_thread_num" and this num changing from time to time depend on number of GPU's connected. I have a start.sh script that checks this number and change "gpu_thread_num" with sed, but i can't figure out how to change "gpu_threads_conf".

I lost my hope trying to figure out how to do it myself and really need your help.

9
  • Instead of modifying the file, why not recreate it each time? Commented Aug 25, 2017 at 22:58
  • @IgnacioVazquez-Abrams it's too large and contains more complex stuff. Commented Aug 25, 2017 at 23:03
  • 1
    If the file is JSON, consider using jq to process it. Commented Aug 25, 2017 at 23:15
  • @Barmar, unfortunately, it's only looking like json, but it is not. Commented Aug 25, 2017 at 23:24
  • Yeah, just noticed the comma after the last element of the array. That's allowed by Javascript, but not JSON. Commented Aug 25, 2017 at 23:25

2 Answers 2

1

If you want to update the number of "gpu_thread_num" according to the number of lines, you can use the following script:

#!/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

it will update directly your config file...

sh Test.sh MyConf.cfg

I hope with one or other solution, you'll find your happiness :)

Sign up to request clarification or add additional context in comments.

1 Comment

This is a perfect solution and it so beautiful that i can't stop to run it back and forth, lol. That's why i was so slow to accept and upvote it. Thank you very much, you're awesome!
0

I'm not sure to have understood well, but you can try something like this:

#!/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

Result:

$ 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 },
],

Just use END with the number of "gpu_thread_num" and I guess that's it.

I hope I understood well the question.

Regards Thomas

2 Comments

For some reason it's not working for me. kusayu@laptop:~/oc_nv.sh$ ./foo.sh ./foo.sh: 5: ./foo.sh: Syntax error: Bad for loop variable
it works with /bin/bash instead of sh. Now i can just delete "gpu_threads_conf" every time from the config file and put it back using the code from your answer. Thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.