How to increment number in a text

2019-08-28 12:59发布

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

标签: bash shell awk
1条回答
戒情不戒烟
2楼-- · 2019-08-28 13:15

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

查看更多
登录 后发表回答