Search code examples
bashawkseduidcmp

Incrementing and modifying numbers on list


I'm trying to write a script as a part of my registration form. Everything is set, however, I would like to implement a "UID" function similar to how UID works in the bash. However here's the tricky part: the UID that I would be implementing would increase by 1 every time a user is added and if that user deletes his / her account his account would be removed from the database thus removing the UID that was used for him. I would like the script that I would be making to check for the existing UIDs and if there's a discrepancy it would fill it.

Example

Name:UID

Apple:0001
Bag:0002
Cat:0003
Dog:0004

Cat deletes his account so the new list would be

Apple:0001
Bag:0002
Dog:0004

and if a new user creates an account it should be

Apple:0001
Bag:0002
NEWUSER:0003
Dog:0004

Everything is already fixed including the deletion of the account from the database I'm just having trouble implementing both the increment of UID and its ability to fill in missing numbers. Much help would be very much appreciated! Thank you so much!

Edit: I have a rough idea of what to use such as sed and cmp while putting them in a looping construct but not good enough to turn them into what I would want.


Solution

  • This will work no matter the order of the input file or how many gaps it has or how big those gaps are:

    $ cat addUser.awk
    BEGIN { FS=OFS=":"; min=max=1 }
    NR==1 { print; next }
    {
        uid = $2+0
        uid2user[uid] = $1
        min = (min > uid ? uid : min)
        max = (max < uid ? uid : max)
    }
    END {
        fmt = "%s" OFS "%04d\n"
        for (uid=min; uid<=max; uid++) {
            if (uid in uid2user) {
                printf fmt, uid2user[uid], uid
            }
            else if (newUser != "") {
                printf fmt, newUser, uid
                newUser = ""
            }
        }
        if (newUser != "") {
            printf fmt, newUser, uid
        }
    }
    

    .

    $ cat users1
    Name:UID
    Cat:0003
    Apple:0001
    Dog:0004
    Bag:0002
    
    $ awk -v newUser="NEWUSER" -f addUser.awk users1
    Name:UID
    Apple:0001
    Bag:0002
    Cat:0003
    Dog:0004
    NEWUSER:0005
    
    $ cat users2
    Name:UID
    Bag:0002
    Dog:0004
    Apple:0001
    
    $ awk -v newUser="NEWUSER" -f addUser.awk users2
    Name:UID
    Apple:0001
    Bag:0002
    NEWUSER:0003
    Dog:0004
    
    $ cat users3
    Name:UID
    
    $ awk -v newUser="NEWUSER" -f addUser.awk users3
    Name:UID
    NEWUSER:0001
    

    .

    $ cat users4
    Name:UID
    Bag:0002
    Dog:0004
    Apple:0001
    Frog:0006
    
    $ awk -v newUser="NEWUSER" -f addUser.awk users4
    Name:UID
    Apple:0001
    Bag:0002
    NEWUSER:0003
    Dog:0004
    Frog:0006
    
    $ cat users5
    Name:UID
    Bag:0002
    Apple:0001
    Frog:0006
    
    $ awk -v newUser="NEWUSER" -f addUser.awk users5
    Name:UID
    Apple:0001
    Bag:0002
    NEWUSER:0003
    Frog:0006