seyshanbe
seyshanbe

Reputation: 41

Edit same emails in csv file

I need to edit csv file. Task is to change first letter of name and surname to uppercase (name surname or name Surname -> Name Surname), I have finished it. But another task is to set emails. Need to set email using name and surname, example ( John Wick -> [email protected]), AND if there another same email (Joe Wick -> [email protected]), need to change second to jwick@mail1.com... if there another one to jwick@mail2.com.

name_new it is a edit names email_new edited email (I haven't finished it) check_same is array there I append all emails (without @mail.ru and number)

while IFS="," read id  name location job email
do
   unset name_new
   unset email_new   
   declare -a check_same
   

    if [ "$id" == id ]; then
        echo "${id},${name},${email}"
        continue
    fi

    for n in $name; do
        if [ -z "$name_new" ]
        then

            name_new=${n^}            #name
            email_new=${n:0:1}        #first letter of name
            email_new=${email_new,,}  #lowercase letter
        else
            name_new+=" ${n^}"        #surname
            email_new+=${n,,}         #lowercase surname for email
            check_same+=($email_new)  #append
         
           #need to add domain @mail.ru or [email protected] [email protected] if it not unique mail name
          
         fi
    done

    echo "${id},${name_new},${email_new}"
done < member.csv

right now statement:

sysadmin@server:~/final$ ./final_1
id,name,email
1,Andriy Marashnichenko,amarashnichenko   (need change to [email protected])
**2,Peter Yan,pyan**        (need change to [email protected])
3,Wesley Brown,wbrown
4,Shuhrat Yusupov,syusupov
5,Nina Kravets,nkravets
6,Inna Shin,ishin
**7,Peter Yan,pyan**         (need change to [email protected])
....
**34,Pavel Yan,pyan**    ([email protected])

I hope you got it. I tryied much more things. I tired to solve it, I don't know what to do.

Upvotes: 0

Views: 67

Answers (2)

glenn jackman
glenn jackman

Reputation: 247022

How about

awk '
    BEGIN {FS = OFS = ","}
    NR > 1 {
        email = $3 (count[$3] ? count[$3] : "") "@mail.ru"
        count[$3]++
        $3 = email
    }
    {print}
' member.csv

Upvotes: 0

tripleee
tripleee

Reputation: 189648

Using a shell loop for this is almost certainly the wrong approach. There are situations where you genuinely cannot avoid it, but this does not seem to be one of those. See also Why is using a shell loop to process text considered bad practice? on our sibling site Unix & Linux.

My suggestion would be to switch to Awk, which has rather similar features for examining and manipulating text.

awk -F "," 'BEGIN { OFS=FS }
  NR>1{
    n = split($2, name, / /); newname = "";
    for (i=1; i<=n; ++i)
        newname = newname " " toupper(substr(name[i], 1, 1)) tolower(substr(name[i], 2))
    if ($2 ~ /(^| )[a-z]/) {
        $2 = substr(newname, 2)
    }
    mailid = tolower(substr(name[1], 1, 1)) tolower(name[n])
    suffix = 0
    while (seen[mailid suffix])
        ++suffix
    ++seen[mailid suffix]
    mailid = mailid (suffix > 0 ? suffix : "")
    $NF = mailid "@mail.ru"
} 1' member.csv

Your requirements were also somewhat inconsistent, but at least I hope this should steer you in the right direction. The output for the sample data you provided is

id,name,email
1,Andriy Marashnichenko,[email protected]
2,Peter Yan,[email protected]
3,Wesley Brown,[email protected]
4,Shuhrat Yusupov,[email protected]
5,Nina Kravets,[email protected]
6,Inna Shin,[email protected]
7,Peter Yan,[email protected]
34,Pavel Yan,[email protected]

(except I replaced mail.ru with example.ru here to avoid exposing the inboxes of innocent third parties to new spam).

In some more detail, NR>1 skips the header line (instead of comparing against id, we trust the first line to be the header). We keep an array seen where each address is kept with a numeric suffix to somewhat simplify the subsequent logic. (The first user will thus be stored as flastname0 in the seen associative array, the second as flastname1 etc). The rest should be fairly obvious; substr("something", start, length) extracts a substring and tolower("STRING") produces a lowercased version. We split the second field (the names) on whitespace into the array name and then piece them back together into a proper-cased version.

Upvotes: 1

Related Questions