-2

I have an array of JSON object with inside local urls and speed parameters like [{server:"192.168.0.100", speed:34}, {server:"192.168.0.130", speed:52},...]
I need to update the speed value for each server..

Reading links like   Replace only if the string is found in a certain context   I've tried this:

#first by first I delete the old value
sed -i 's/speed:\(.\), //g' $FILENAME
sed -i 's/speed:\(..\), //g' $FILENAME
sed -i 's/speed:\(...\), //g' $FILENAME
sed -i 's/speed:\(....\), //g' $FILENAME

#then I've tried to calculate the new one
sed -i "s/server:\"\(192.168.0...\)\"/server:\"\1\", speed:$( ping -c3 \\1 | grep rtt | cut -f 5 -d '/' ), /g" $FILENAME

But it doesn't work:

unknown host \1

  • not sure if one can pass captured group in sed to external command... – Sundeep Nov 03 '16 at 11:53
  • 1
    The shown data format isn't valid json. Maybe you should elaborate why you have chosen this format and not csv or whatsoever... – FloHimself Nov 03 '16 at 11:55
  • why is not valid?? Anyway I don't mind the format or the use of sed but I need it in a text file. So If you have an other idea to replace/update that value please help me – lunix15 Nov 03 '16 at 12:37
  • @lunix15 Strings should be wrapped in double quotes. – FloHimself Nov 03 '16 at 13:19
  • ah ok.. but it doesn't matter in javascript when you read json. Anyway thanks – lunix15 Nov 03 '16 at 16:03
  • Please show us your expected input and desired output. The approach you are using is extraordinarily complicated for something so simple; I can't even tell what you're trying to do. – Wildcard Nov 03 '16 at 16:27
  • think about a simple GUI showing the last speed for each server and so a script that updates those speeds every x seconds.. so the starting value is like [{server:"192.168.0.100", speed:34}, {server:"192.168.0.130", speed:52}] that have to be update.. so it could become for example like [{server:"192.168.0.100", speed:21}, {server:"192.168.0.130", speed:94}] evaluate by ping test – lunix15 Nov 10 '16 at 15:24
  • The JSON that you're sowing is unfortunately not valid. The keys should be quoted. – Kusalananda Jul 13 '21 at 06:45
  • no need to quote keys in javascript – lunix15 Jul 14 '21 at 08:03

1 Answers1

-2

Yeah, I've found a solution using a mix of arrays and sed..:

#delete the old values
sed -i 's/speed:\(.\), //g' "$FILENAME"
sed -i 's/speed:\(..\), //g' "$FILENAME"
sed -i 's/speed:\(...\), //g' "$FILENAME"
sed -i 's/speed:\(....\), //g' "$FILENAME"

#create an array with just the urls
SERVER=( $(sed -nre 's/.*server:"(.*)".*/\1/p' "$FILENAME") )

#for each element
for element in $(seq 0 $((${#SERVER[@]}-1)))
do
    #calculate the new average ping speed
    SPEED[element]="$( sudo ping -c2 "${SERVER[element]}" | grep rtt | cut -f 5 -d '/' )"

    #if server is not available
    if [ -z "${SPEED[element]}" ]; then SPEED[element]=1000 ; fi

    #add the old server speed
    sed -i "s/\"\(${SERVER[element]}\)\"/\"\1\", speed:${SPEED[element]}/g" "$FILENAME"
done

Maybe is not performing but it works and could be usefull