change string/value in 3rd column based on value change in 6th column - awk

Tags:

Answer: 1

10 hours ago

I am trying to figure out how to use awk to force the change of consecutive numbering in 3rd column of my file based on the value change in my 6th column, but preserving the spacing?? Can somebody help?

Here is a example:

0.00    47258      0   33103.5   33051.0    -9.20
0.00    47258      0   33103.5   33051.0    -9.20
0.00    47258      0   34329.3   34185.7   -55.44
0.00    47258      0   34329.3   34185.7   -55.44
0.00    47258      0   32677.6   32493.5    30.22
0.00    47258      0   32677.6   32493.5    30.22

And I want to achieve this:

0.00    47258      1   33103.5   33051.0    -9.20
0.00    47258      1   33103.5   33051.0    -9.20
0.00    47258      2   34329.3   34185.7   -55.44
0.00    47258      2   34329.3   34185.7   -55.44
0.00    47258      3   32677.6   32493.5    30.22
0.00    47258      3   32677.6   32493.5    30.22

Answer: 2

28 hours ago

How about

awk '$6 != last {++n; last = $6} {$3 = n} 1'

Ex.

$ awk '$6 != last {++n; last = $6} {$3 = n} 1' file
0.00 47258 1 33103.5 33051.0 -9.20
0.00 47258 1 33103.5 33051.0 -9.20
0.00 47258 2 34329.3 34185.7 -55.44
0.00 47258 2 34329.3 34185.7 -55.44
0.00 47258 3 32677.6 32493.5 30.22
0.00 47258 3 32677.6 32493.5 30.22
Added by: Aditya Ernser

Popular Search

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9