Replacing string by incrementing number

Dear all
Say I have a file as

 
! TICKET NBR : 234 !GSI : 102 ! 3100.2.112.1 11/06/2013 15:56:29 ! 3100.2.22.3 98 ! 3100.2.134.2 8 !
! TICKET NBR : 1809 ! GSI : 102 ! 3100.2.112.1 11/06/2013 16:00:45 ! 3100.2.22.3 65 ! 3100.2.134.2 3 !
! TICKET NBR : 587 ! GSI : 102 ! 3100.2.112.1 11/06/2013 16:00:45 ! 3100.2.22.3 45 ! 3100.2.134.2 6 ! 

It would be great, if i could issue a script (Vi/Bash/awk, ...) in order to obtain

! TICKET NBR : 1 !GSI : 102 ! 3100.2.112.1 11/06/2013 15:56:29 ! 3100.2.22.3 98 ! 3100.2.134.2 8 !
! TICKET NBR : 2 ! GSI : 102 ! 3100.2.112.1 11/06/2013 16:00:45 ! 3100.2.22.3 65 ! 3100.2.134.2 3 !
! TICKET NBR : 3 ! GSI : 102 ! 3100.2.112.1 11/06/2013 16:00:45 ! 3100.2.22.3 45 ! 3100.2.134.2 6 !

So, the "replace string" should be replaced by another string containing the counter of replacement.
Could you help me with this?

Thanks a lot.

How about this:

awk 'NF > 4 {$5 = ++N} 1' infile
1 Like

Someone already posted a very elegant awk solution, so this is just because I like trying to solve things in pure shell.

i=1;while read -a line;do line[4]=$i;echo "${line[@]}";let i=i+1;done < infile
1 Like

awesome ,