Juan Jor Orellana Juan Jor Orellana - 12 days ago 5
Linux Question

Replace/Update column value from text document in bash

I got a question on how can i update an specific column value from a row in a text document in bash.
So far I do this:
In this case I'm trying to update the 4th column in a line from an specific account number. take the current value, sum with a new value inserted by the user and then replace that current value with the result.

Open the script with two variable $1 holds the account I ll be looking for while $2 the file name.

The text document has information like this:

11101 : CAJA GENERAL : 111 : 0
11102 : CAJA CHICA : 111 : 0
112 : BANCOS : 11 : 0
11201 : CUENTAS CORRIENTES : 112 : 0
1120101 : Banco Agrícola S.A. : 11201 : 20
1120102 : Banco Hipotecario S.A. : 11201 : 0
11202 : CUENTAS DE AHORRO : 112 : 0
1120201 : Banco Agrícola S.A. : 11202 : 0


I use this code to find the correct row and assign the current value to the var "act" and aggregate it with another new value

read -p "Inserte monto" insert
act=$(grep -w "^$1" $2 | cut -d":" -f4)
vare=$(($act + $insert))


But then I need to place this new value to the exact column/row I took the original value.

How do i exactly do it? I'm quite exhausted from a long day of traveling and now I want to finish this and go to bed. Anyone could give me an idea or solution? I'd really appreciate any help right now.

EDIT:
Ok.. I went and try AWK.
Found this to "replace" text

awk -F':' -vOFS=' ' '{ $4 = "$vare"}1'


So far... once I enter that line the execution of the script stops.. or I dont know what but it doesn't continue anymore, nor shows any error.
Am I doing anything wrong?

EDIT 2:
Expected Input.
After a successful update I want the selected account to have the updated value in a existing document in my directory, the solutions so far allow me to see the updates within terminal, but is the original document the one I need to see the changes. Thanks Ed Morton for the tip

Answer
Maybe try awk:

$ cat file
11101   :   CAJA GENERAL            :   111     :   0
11102   :   CAJA CHICA              :   111     :   0
112     :   BANCOS                  :   11      :   1
11201   :   CUENTAS CORRIENTES      :   112     :   0
1120101 :   Banco Agrícola S.A.     :   11201   :   20
1120102 :   Banco Hipotecario S.A.  :   11201   :   0
11202   :   CUENTAS DE AHORRO       :   112     :   0
1120201 :   Banco Agrícola S.A.     :   11202   :   0

$ insert=10

$ ident=112

$ awk -i inplace -F: "/^$ident /{gsub(/([0-9]+)$/, \$4+ $insert)};{print}" file

$ cat file
11101   :   CAJA GENERAL            :   111     :   0
11102   :   CAJA CHICA              :   111     :   0
112     :   BANCOS                  :   11      :   11
11201   :   CUENTAS CORRIENTES      :   112     :   0
1120101 :   Banco Agrícola S.A.     :   11201   :   20
1120102 :   Banco Hipotecario S.A.  :   11201   :   0
11202   :   CUENTAS DE AHORRO       :   112     :   0
1120201 :   Banco Agrícola S.A.     :   11202   :   0