linux - awk conditional line numbering -


i'm trying put numbers before each line in file if value in second column (field) different preceding

input file:

11:10pm *jane> login 11:15pm *bob>  login 11:16pm *bob>  logout 11:45pm *betty> login 01:20am *john>  login 

output:

1. 11:10pm *jane> login 2. 11:15pm *bob>  login    11:16pm *bob>  logout 3. 11:45pm *betty> login 4. 01:20am *john> login 

takes advantage of fact variables have default values in awk:

{   if (last == $2) {     printf("    %s\n", $0)   } else {     n += 1;     last = $2;     printf("%2d. %s\n", n, $0);   } } 

this uses n remember how many entries have been seen , last remember last entry seen.


Comments