A simple way to do it is maintain an array using the username as the index and increment it each time you read a user, e.g.
awk '{ print (($1 in a) ? $1 a[$1] : $1) "@email.com"; a[$1]++ }' file
The ternary (($1 in a) ? $1 a[$1] : $1)
just checks if the user in in a[]
yet, and if so uses the name plus the value of the array $1 a[$1]
if the user is not in the array, then it just uses the user $1
. The result of the ternary is concatenated with "@email.com"
to complete the output.
Lastly, the value for the array element for the user is incremented, a[$1]++
.
Example Use/Output
With your names in a file called users
you would have:
$ awk '{ print (($1 in a) ? $1 a[$1] : $1) "@email.com"; a[$1]++ }' users
jsmith@email.com
jsmith1@email.com
kgonzales@email.com
shouston@email.com
dgenesy@email.com
kgonzales1@email.com
jsmith2@email.com
To Keep E-mail In Input File
If your input already contains an e-mail at the end of the username, then you simply want to output that record and skip to the next record, e.g.
awk '$1~/@/{print; next} { print (($1 in a) ? $1 a[$1] : $1) "@email.com"; a[$1]++ }' users
That will preserve e.meeks@example.or
from your comment.
Example Input
jsmith
jsmith
kgonzales
shouston
e.meeks@example.org
dgenesy
kgonzales
jsmith
Example Output
jsmith@email.com
jsmith1@email.com
kgonzales@email.com
shouston@email.com
e.meeks@example.org
dgenesy@email.com
kgonzales1@email.com
jsmith2@email.com
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…