Untitled

 avatar
unknown
plain_text
a month ago
2.3 kB
2
Indexable
#!/bin/bash

if [ -z "$1" ]; then
    echo "Please provide the path to the accounts.csv file."
    exit 1
fi

input="$1"
output="accounts_new.csv"

if [ ! -f "$input" ]; then
    echo "The file $input does not exist."
    exit 1
fi

# Initialize the email count array
declare -A email_count

# Step 1: Copy the header from the input to the output file without modification
head -n 1 "$input" > "$output"

# Step 2: First pass to count occurrences of email bases
while IFS=',' read -r id location name title email department; do
    # Skip the header row
    if [[ "$id" == "id" ]]; then
        continue
    fi

    # Format name parts (first and last)
    name_parts=($name)
    first="${name_parts[0]}"
    last="${name_parts[@]:1}"

    # Capitalize first and last names
    first_formatted="${first^}"
    last_formatted="${last^}"

    # Create email base (first initial + last name)
    email_base="${first_formatted:0:1}${last_formatted}"
    email_base="${email_base,,}" # Convert to lowercase

    # Count occurrences of email base
    ((email_count["$email_base"]++))
done < "$input"

# Step 3: Second pass to write processed data
declare -A email_generated

while IFS=',' read -r id location name title email department; do
    # Skip the header row
    if [[ "$id" == "id" ]]; then
        continue
    fi

    # Format name parts (first and last)
    name_parts=($name)
    first="${name_parts[0]}"
    last="${name_parts[@]:1}"

    # Capitalize first and last names
    first_formatted="${first^}"
    last_formatted="${last^}"

    # Create email base (first initial + last name)
    email_base="${first_formatted:0:1}${last_formatted}"
    email_base="${email_base,,}" # Convert to lowercase

    # Generate a unique email
    if ((email_count["$email_base"] > 1)); then
        # Add location ID to email to make it unique
        full_email="${email_base}${location}@abc.com"
    else
        full_email="${email_base}@abc.com"
    fi

    # Clean title and department (preserve commas and quote the fields if needed)
    title_cleaned="$title"
    department_cleaned="$department"

    # Write the processed data to the output file
    echo "$id,$location,$first_formatted $last_formatted,$title_cleaned,$full_email,$department_cleaned" >> "$output"
done < "$input"

echo "New file created: $output"
Leave a Comment