jeudi 28 mai 2020

Bash: rename duplicates for generated values in csv file

There is a bash script that create a email addresses in next format: first letter from name and full surname, lowercase +@example.com.

csv file:

id,location,name,email
1,1,John Smith,ab@dc.com
2,2,Paul Robinson,
3,3,Fidel Guererro,qw@er.com
4,4,John Smith,
...

Column Name can contain duplicates. In this case script should add 1 in email address (ex. for id=1 - jsmith@example.com, for id=4 - jsmith1@example.com). I try next script:

#!/bin/bash

while IFS=, read -r col1 col2 col3 col4
do
            if [ "$col1" == Id ]; then
                echo "${col1},${col2},${col3},${col4}"
                continue
            fi

    firstinitial=${col3:0:1}
    surname=$(echo $col3 | cut -d' ' -f2)

            if [[ $col4 ==  $col4 ]]; then
                col4=${firstinitial,}${surname,,}1@abc.com
                else
                col4=${firstinitial,}${surname,,}@abc.com
            fi

    echo "${col1},${col2},${col3},${col4}"
done < acc.csv

I recieve all addresses with 1. How can I change this script?

Aucun commentaire:

Enregistrer un commentaire