bashawksed

increment a column and line number value using bash


I have a tab-delimited file that contains 4 columns

numbers.txt

0       0       0       0
0       0       0       0
0       0       0       0
0       0       0       0

In bash I need to be able to increment a specific column and line by 1, so for example +1 to column 2 line 3 to make it

0       0       0       0
0       0       0       0
0       1       0       0
0       0       0       0

I can get a specific column and line using

cat test | awk '{print $2}' | sed '3!d;q'

which returns 1 as expected but I can't work out how to update the value in a specific column/line i.e.

cat test | awk '{print $2}' | sed '3!d;q' | +1 to the value in this column

Solution

  • This awk should work for you:

    awk -v r=3 -v c=2 'BEGIN {FS=OFS="\t"} NR == r {++$c} 1' file
    
    0   0   0   0
    0   0   0   0
    0   1   0   0
    0   0   0   0