kusayu
kusayu

Reputation: 93

how to edit multiple lines using script

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.

Upvotes: 0

Views: 159

Answers (2)

Totoc1001
Totoc1001

Reputation: 356

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 :)

Upvotes: 1

Totoc1001
Totoc1001

Reputation: 356

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

Upvotes: 0

Related Questions