Replacement of certain values in the column for variables



  • I have a file consisting of a lot of lines and poles. Below the piece where replacement is required)

    We need to change the value in the 5th column to the variable (calculated manually (number 0 to infinity)).

    I've been through SED, but he's sometimes altering the next few columns, apparently because of sometimes overlaps.

    line_no_1=`cat -n /home/user1/test.txt| grep 1.node|grep topon.S1| awk '{print $1}'` #ищу номер строки 
    weight_1=`cat -n /home/user1/test.txt| grep 1.node|grep topon.S1| awk '{print $6}'` #ищу значение в столбце
    

    read new_weight_1

    sed -i "${line_no_1}"'s/'$weight_1'/'$new_weight_1'/' /home/user1/test.txt

    I tried to change the line on a changed line, too.

    number_line_no_1=cat -n /home/user1/test.txt| grep 1.node|grep topon.S1 awk '{print $1}' #ищу номер строки
    weight_1=cat -n /home/user1/test.txt| grep 1.node|grep topon.S1| awk '{print $6}' #ищу значение в столбце

    line_no_1=cat /home/user1/test.txt| grep 1.node|grep topon.S1 # записываю строку в
    переменную

    read new_weight_1

    a=cat /home/user1/test.txt| grep 1.node|grep topon.S1| awk '$5="'$new_weight_1'"' #
    строчка с изменеённым весом

    sed -i "${number_line_no_1}"'s/'$line_no_1'/'$a'/' /home/user1/test.txt

    исходный файл



  • You might be interested in this scenario:

    #! /bin/bash
    

    FILENAME="./test.txt"

    declare -i nNode=1
    declare new_weight=

    read -r new_weight

    awk -v nNode="${nNode}" -v new_weight="${new_weight}" '
    BEGIN {
    # Разделитель выходных столбцов будет табуляцией
    OFS="\t"
    }
    # Фильтр по значениям первого и последнего столбцов
    $1 == (nNode ".node") && $7 == ("topon.S" nNode) {
    # записать новое значение в столбец веса ...
    $5 = new_weight
    # ...и линия печати...
    print
    # ...и вычислить следующую строку
    next
    }
    {
    # переформатировать строку с помощью OFS...
    $1 = $1
    # ...и линия печати
    print
    }
    '
    < "${FILENAME}"
    > "${FILENAME}.tmp"

    mv "${FILENAME}.tmp" "${FILENAME}"



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2