У меня такое задание: create a bash code based on this info: company ABC has uncontrolled process of employee’s accounts creation. Currently process means adding names, email and other personal data manually to the account.csv file without any rules. Department lead has decided to improve it based on the naming convention implementation. Good idea for newcomers, but what to do with current user’s list?
You have been asked to help. Could you please develop automated way (bash script) and create new accounts_new.csv file based on current accounts.csv and following:
- Need to update column name. Name format: first letter of name/surname uppercase and all other letters lowercase.
- Need to update column email with domain abc. Email format: first letter from name and full surname, lowercase. Equals emails should contain location_id.
- Scripts should has name task1.sh
- Path to account.csv file should be as argument to the script.
Definition of done: Developed bash script which automatically creates accounts_new.csv and updates columns name and email based on the rules above.
Script should not use any additional installed packages!
Example: ./task.sh accounts.csv
Was: 2,1,Christina Gonzalez,Director,, 8,6,Bart Charlow,Executive Director,, 9,7,Bart Charlow,Executive Director,,
Became: 2,1,Christina Gonzalez,Director,cgonzalez@abc.com, 8,6,Bart Charlow,Executive Director,bcharlow6@abc.com, 9,7,Bart Charlow,Executive Director,bcharlow7@abc.com,
Мой Bash Script code:
#!/bin/bash
# Check if filename argument is provided
if [ $# -ne 1 ]; then
echo "Usage: $0 accounts.csv"
exit 1
fi
# Check if the provided file exists
if [ ! -f "$1" ]; then
echo "File '$1' not found!"
exit 1
fi
# Generate header line
echo "id,location_id,name,title,email,department"
# Function to convert name to lowercase with first letter uppercase
convert_name() {
echo "$1" | sed 's/.*/\L&/; s/[a-z]*/\u&/g'
}
# Function to generate email based on name and surname
generate_email() {
local name=$(convert_name "$1")
local surname=$(convert_name "$2")
local location_id="$3"
if [ -z "$location_id" ]; then
echo "${name:0:1}${surname}@abc.com," | tr '[:upper:]' '[:lower:]'
else
echo "${name:0:1}${surname}${location_id}@abc.com," | tr '[:upper:]' '[:lower:]'
fi
}
# Main logic to process the CSV file
while IFS=',' read -r col1 col2 col3 col4 col5 col6; do
# Skip header if present
if [ "$col1" == "ID" ]; then
echo "$col1,$col2,$col3,$col4,$col5"
continue
fi
# Extracting name and surname from col3
IFS=' ' read -r firstname lastname <<< "$col3"
# Updating name format
updated_name=$(convert_name "$firstname")" "$(convert_name "$lastname")
# Generating updated email
updated_email=$(generate_email "$firstname" "$lastname" "$col6")
# Printing the updated line
echo "$col1,$col2,$updated_name,$col4,$updated_email"
done < "$1" > accounts_new.csv
echo "New file created: accounts_new.csv"
Прогоняя через тест получаю следующую ошибку:
At index 0 diff: 'id,location_id,Name ,title,ndepartment@abc.com,\n' != 'id,location_id,name,title,email,department\n'
Full diff:
[
- 'id,location_id,name,title,email,department\n',
? ^ ^^^^^^
+ 'id,location_id,Name ,title,ndepartment@abc.com,\n',
? ^ + ^ +++++++++
- '1,1,Susan Houston,Director of Services,shouston1@abc.com,\n',
? -
+ '1,1,Susan Houston,Director of Services,shouston@abc.com,\n',
'2,1,Christina Gonzalez,Director,cgonzalez@abc.com,\n',
- '3,2,Brenda Brown,"Director, Second Career Services",bbrown@abc.com,\n',
? -------------------------
+ '3,2,Brenda Brown,"Director,bbrown@abc.com,\n',
- '4,3,Howard Lader,"Manager, Senior Counseling",hlader@abc.com,\n',
? --------------------
+ '4,3,Howard Lader,"Manager,hlader@abc.com,\n',
'5,4,Kimberly Pesavento,Commercial director,kpesavento@abc.com,\n',
'6,5,Joe Bloom,Financial Empowerment Programs Program '
'Director,jbloom@abc.com,\n',
'7,6,Peter Olson,Director,polson@abc.com,\n',
- '8,6,Bart Charlow,Executive Director,bcharlow6@abc.com,\n',
? -
+ '8,6,Bart Charlow,Executive Director,bcharlow@abc.com,\n',
- '9,7,Bart Charlow,Executive Director,bcharlow7@abc.com,\n',
? -
+ '9,7,Bart Charlow,Executive Director,bcharlow@abc.com,\n',
'10,7,Barbara Kalt,Director,bkalt@abc.com,\n',
'11,8,Marilyn Baker-Venturini,Director,mbaker-venturini@abc.com,\n',
'12,8,Graciela Hernandez,Assistant Manager,ghernandez@abc.com,\n',
'13,8,Julie Avelino,Assessment Specialist,javelino@abc.com,\n',
- '14,9,Dave Genesy,Library Director,dgenesy9@abc.com,\n',
? -
+ '14,9,Dave Genesy,Library Director,dgenesy@abc.com,\n',
- '15,9,Maria Kramer,Library Divisions Manager,mkramer9@abc.com,\n',
? -
+ '15,9,Maria Kramer,Library Divisions Manager,mkramer@abc.com,\n',
- '16,10,Dave Genesy,Tester,dgenesy10@abc.com,\n',
? --
+ '16,10,Dave Genesy,Tester,dgenesy@abc.com,\n',
- '17,10,Maria Kramer,Library Division Manager,mkramer10@abc.com,\n',
? --
+ '17,10,Maria Kramer,Library Division Manager,mkramer@abc.com,\n',
- '18,11,Dave Genesy,Head of office,dgenesy11@abc.com,\n',
? --
+ '18,11,Dave Genesy,Head of office,dgenesy@abc.com,\n',
- '19,11,Elizabeth Meeks,Branch Manager,emeeks11@abc.com,\n',
? --
+ '19,11,Elizabeth Meeks,Branch Manager,emeeks@abc.com,\n',
'20,12,Kathy Endaya,Director,kendaya@abc.com,\n',
- '21,13,Dave Genesy,Library Director,dgenesy13@abc.com,\n',
? --
+ '21,13,Dave Genesy,Library Director,dgenesy@abc.com,\n',
- '22,14,Andres Espinoza,"Manager, Commanding Officer",aespinoza@abc.com,\n',
? ---------------------
+ '22,14,Andres Espinoza,"Manager,aespinoza@abc.com,\n',
'23,15,Jack Phillips,Administrator,jphillips@abc.com,\n',
'24,16,James Lee,Commanding Officer,jlee@abc.com,\n',
'25,17,Kenneth Gibson,Tester,kgibson@abc.com,\n',
- '26,18,Sharon Petersen,Administrator,spetersen18@abc.com,\n',
? --
+ '26,18,Sharon Petersen,Administrator,spetersen@abc.com,\n',
- '27,19,Sharon Petersen,Administrator,spetersen19@abc.com,\n',
? --
+ '27,19,Sharon Petersen,Administrator,spetersen@abc.com,\n',
- '28,21,Moncef Salah,Tester,msalah@abc.com,Office of Innovation\n',
+ '28,21,Moncef Salah,Tester,msalahoffice of innovation@abc.com,\n',
- '29,22,Suzanne Badenhoop,Tester,sbadenhoop@abc.com,Referrals\n',
? ---------
+ '29,22,Suzanne Badenhoop,Tester,sbadenhoopreferrals@abc.com,\n',
? +++++++++
- '30,20,Sean Houston,Director of new Services,shouston20@abc.com,\n',
? --
+ '30,20,Sean Houston,Director of new Services,shouston@abc.com,\n',
- '31,8,David Genesy,Account Manager,dgenesy8@abc.com,\n',
? -
+ '31,8,David Genesy,Account Manager,dgenesy@abc.com,\n',
- '32,8,Elizabeth Feeney,CEO,efeeney@abc.com,Operations\n',
? ----------
+ '32,8,Elizabeth Feeney,CEO,efeeneyoperations@abc.com,\n',
? ++++++++++
- '33,8,Erika Meeks,Tester,emeeks8@abc.com,Operations\n',
? ^ ----------
+ '33,8,Erika Meeks,Tester,emeeksoperations@abc.com,\n',
? ^^^^^^^^^^
]
E At index 0 diff: 'id,location_id,Name ,title,ndepartment@abc.com,\n' != 'id,location_id,name,title,email,department\n'
E
E Full diff:
E [
E - 'id,location_id,name,title,email,department\n',
E ? ^ ^^^^^^
E + 'id,location_id,Name ,title,ndepartment@abc.com,\n',
E ? ^ + ^ +++++++++
E - '1,
Что бы я не делал эта ошибка выходит:
At index 0 diff: 'id,location_id,Name ,title,ndepartment@abc.com,\n' != 'id,location_id,name,title,email,department\n'
Full diff:
[
- 'id,location_id,name,title,email,department\n',
? ^ ^^^^^^
+ 'id,location_id,Name ,title,ndepartment@abc.com,\n',
? ^ + ^ +++++++++
Я немного изменил код. Теперь он выглядит так:
#!/bin/bash
# Check if filename argument is provided
if [ $# -ne 1 ]; then
echo "Usage: $0 accounts.csv"
exit 1
fi
# Check if the provided file exists
if [ ! -f "$1" ]; then
echo "File '$1' not found!"
exit 1
fi
# Function to convert name to lowercase with first letter uppercase
convert_name() {
local a=${1:0:1}
local b=${1:1}
echo ${a^}${b,,}
}
# Function to generate email based on name and surname
generate_email() {
local name=$(convert_name "$1")
local surname=$(convert_name "$2")
local location_id="$3"
name=$(echo "$name" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
surname=$(echo "$surname" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [ -z "$location_id" ]; then
echo "${name:0:1}${surname}@abc.com," | tr '[:upper:]' '[:lower:]'
else
echo "${name:0:1}${surname}${location_id}@abc.com," | tr '[:upper:]' '[:lower:]'
fi
}
# Update header line in the output file
echo "id,location_id,name,title,email,department" > accounts_new.csv
# Main logic to process the CSV file
while IFS=',' read -r col1 col2 col3 col4 col5 col6; do
# Skip header if present
if [ "$col1" == "id" ]; then
continue
fi
# Extracting name and surname from col3
IFS=' ' read -r firstname lastname <<< "$col3"
# Updating name format
updated_name=$(convert_name "$firstname")" "$(convert_name "$lastname")
# Generating updated email
updated_email=$(generate_email "$firstname" "$lastname" "$col6")
# Printing the updated line
echo "$col1,$col2,$updated_name,$col4,$updated_email"
done < "$1" >> accounts_new.csv
echo "New file created: accounts_new.csv"
Вот что содержит ‘Failure message’:
At index 1 diff: '1,1,Susan Houston,Director of Services,shouston@abc.com,\n' != '1,1,Susan Houston,Director of Services,shouston1@abc.com,\n'
Full diff:
[
'id,location_id,name,title,email,department\n',
- '1,1,Susan Houston,Director of Services,shouston1@abc.com,\n',
? -
+ '1,1,Susan Houston,Director of Services,shouston@abc.com,\n',
'2,1,Christina Gonzalez,Director,cgonzalez@abc.com,\n',
- '3,2,Brenda Brown,"Director, Second Career Services",bbrown@abc.com,\n',
? -------------------------
+ '3,2,Brenda Brown,"Director,bbrown@abc.com,\n',
- '4,3,Howard Lader,"Manager, Senior Counseling",hlader@abc.com,\n',
? --------------------
+ '4,3,Howard Lader,"Manager,hlader@abc.com,\n',
'5,4,Kimberly Pesavento,Commercial director,kpesavento@abc.com,\n',
'6,5,Joe Bloom,Financial Empowerment Programs Program '
'Director,jbloom@abc.com,\n',
'7,6,Peter Olson,Director,polson@abc.com,\n',
- '8,6,Bart Charlow,Executive Director,bcharlow6@abc.com,\n',
? -
+ '8,6,Bart Charlow,Executive Director,bcharlow@abc.com,\n',
- '9,7,Bart Charlow,Executive Director,bcharlow7@abc.com,\n',
? -
+ '9,7,Bart Charlow,Executive Director,bcharlow@abc.com,\n',
'10,7,Barbara Kalt,Director,bkalt@abc.com,\n',
- '11,8,Marilyn Baker-Venturini,Director,mbaker-venturini@abc.com,\n',
? ^
+ '11,8,Marilyn Baker-venturini,Director,mbaker-venturini@abc.com,\n',
? ^
'12,8,Graciela Hernandez,Assistant Manager,ghernandez@abc.com,\n',
'13,8,Julie Avelino,Assessment Specialist,javelino@abc.com,\n',
- '14,9,Dave Genesy,Library Director,dgenesy9@abc.com,\n',
? -
+ '14,9,Dave Genesy,Library Director,dgenesy@abc.com,\n',
- '15,9,Maria Kramer,Library Divisions Manager,mkramer9@abc.com,\n',
? -
+ '15,9,Maria Kramer,Library Divisions Manager,mkramer@abc.com,\n',
- '16,10,Dave Genesy,Tester,dgenesy10@abc.com,\n',
? --
+ '16,10,Dave Genesy,Tester,dgenesy@abc.com,\n',
- '17,10,Maria Kramer,Library Division Manager,mkramer10@abc.com,\n',
? --
+ '17,10,Maria Kramer,Library Division Manager,mkramer@abc.com,\n',
- '18,11,Dave Genesy,Head of office,dgenesy11@abc.com,\n',
? --
+ '18,11,Dave Genesy,Head of office,dgenesy@abc.com,\n',
- '19,11,Elizabeth Meeks,Branch Manager,emeeks11@abc.com,\n',
? --
+ '19,11,Elizabeth Meeks,Branch Manager,emeeks@abc.com,\n',
'20,12,Kathy Endaya,Director,kendaya@abc.com,\n',
- '21,13,Dave Genesy,Library Director,dgenesy13@abc.com,\n',
? --
+ '21,13,Dave Genesy,Library Director,dgenesy@abc.com,\n',
- '22,14,Andres Espinoza,"Manager, Commanding Officer",aespinoza@abc.com,\n',
? ---------------------
+ '22,14,Andres Espinoza,"Manager,aespinoza@abc.com,\n',
'23,15,Jack Phillips,Administrator,jphillips@abc.com,\n',
'24,16,James Lee,Commanding Officer,jlee@abc.com,\n',
'25,17,Kenneth Gibson,Tester,kgibson@abc.com,\n',
- '26,18,Sharon Petersen,Administrator,spetersen18@abc.com,\n',
? --
+ '26,18,Sharon Petersen,Administrator,spetersen@abc.com,\n',
- '27,19,Sharon Petersen,Administrator,spetersen19@abc.com,\n',
? --
+ '27,19,Sharon Petersen,Administrator,spetersen@abc.com,\n',
- '28,21,Moncef Salah,Tester,msalah@abc.com,Office of Innovation\n',
+ '28,21,Moncef Salah,Tester,msalahoffice of innovation@abc.com,\n',
- '29,22,Suzanne Badenhoop,Tester,sbadenhoop@abc.com,Referrals\n',
? ---------
+ '29,22,Suzanne Badenhoop,Tester,sbadenhoopreferrals@abc.com,\n',
? +++++++++
- '30,20,Sean Houston,Director of new Services,shouston20@abc.com,\n',
? --
+ '30,20,Sean Houston,Director of new Services,shouston@abc.com,\n',
- '31,8,David Genesy,Account Manager,dgenesy8@abc.com,\n',
? -
+ '31,8,David Genesy,Account Manager,dgenesy@abc.com,\n',
- '32,8,Elizabeth Feeney,CEO,efeeney@abc.com,Operations\n',
? ----------
+ '32,8,Elizabeth Feeney,CEO,efeeneyoperations@abc.com,\n',
? ++++++++++
- '33,8,Erika Meeks,Tester,emeeks8@abc.com,Operations\n',
? ^ ----------
+ '33,8,Erika Meeks,Tester,emeeksoperations@abc.com,\n',
? ^^^^^^^^^^
]