Untitled
#!/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 # Write the header to the output file (without any modification) head -n 1 "$input" > "$output" # First pass through the file to count occurrences of email base { read # Skip the header line # Process the file and count email occurrences awk -F',' 'BEGIN {OFS=","} { if (NF > 0) { id = $1; location = $2; name = $3; title = $4; email = $5; department = $6; # Format name parts (first and last) split(name, name_parts, " "); first = name_parts[1]; last = ""; for (i = 2; i <= length(name_parts); i++) { last = last " " name_parts[i]; } # Capitalize first and last names first_formatted = toupper(substr(first,1,1)) tolower(substr(first,2)); last_formatted = toupper(substr(last,1,1)) tolower(substr(last,2)); # Create email base (first initial + last name) email_base = tolower(substr(first_formatted,1,1) substr(last_formatted,1)); # Count the occurrences of the email base email_count[email_base]++; } }' < "$input" } >> "$output" # Append to the output file # Second pass through the file to process and generate unique emails { read # Skip the header line # Now process the file for output awk -F',' 'BEGIN {OFS=","} { if (NF > 0) { id = $1; location = $2; # Determine if email is unique or not if (email_count[email_base] > 1) { full_email = email_base location "@abc.com"; # If not unique, add location ID } else { full_email = email_base "@abc.com"; # If unique, use base email } # Clean title and department (preserve commas and quote the fields if needed) title_cleaned = title; department_cleaned = department; # If title contains commas, quote it if (index(title_cleaned, ",") > 0) { title_cleaned = "\"" title_cleaned "\""; } # If department contains commas, quote it if (index(department_cleaned, ",") > 0) { department_cleaned = "\"" department_cleaned "\""; } # Write the processed data to the output file print id "," location "," first_formatted " " last_formatted "," title_cleaned "," full_email "," department_cleaned; } }' < "$input" >> "$output" # Append the processed data to the output file } echo "New file created: $output"
Leave a Comment