How to increment number in a text

2019-08-28 13:16发布

问题:

I'm trying to increase the last character in the third row

This is my input file

33981 Juan Robles Garcia 5 1
33982 Lucas Robles Garcia 3 0
33983 Alba Robles Garcia 3 0

This would be the output file

33981 Juan Robles Garcia 5 1
33982 Lucas Robles Garcia 3 0
33983 Alba Robles Garcia 3 1

回答1:

If current row is third row (NR==3) then increment value by one in last column ($NF). In any case, output the whole row (print).

In any case, prints the entire line .

awk '{if (NR==3) {$NF++}; {print}}' file

or shorter

awk 'NR==3 {$NF++} {print}' file

or shorter

awk 'NR==3 {$NF++}1' file

Output:

33981 Juan Robles Garcia 5 1
33982 Lucas Robles Garcia 3 0
33983 Alba Robles Garcia 3 1

See: 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR



标签: bash shell awk