Code:
awk -F: -v OFS=: '{ $3=$4=($3+a[$3]++) }1' <( sort -t : -k3n /etc/passwd )
First, the input needs to be sorted, so I used a bash
process substitution to dynamically create the sorted "file".
awk's -F and -v options set the input and output field separators to ":".
$3=$4=() changes fields 3 and 4 (I'm assuming that
gid needs to be incremented too) to the new value calculated inside the parentheses.
$3+a[$3]++ adds the current value of the array entry "
a[$3]" to the value of field three, then post-increments the array value by one ("
a[$3]++"). This means that the first time you encounter a given value in field 3,
a[$3] will be zero, and one higher for every time encountered after that.
The final "
1" at the end is a shortcut trick for
printing the current line after the modifications have been made.
awk only outputs to stdout, so you'll have to save it to a temp file and replace it after that.
Edit: Note however that this is just a quick and brain-dead solution. As it stands it only increments the
uid number by one and doesn't do any testing against subsequent conflicts.