Have a script that parses csv file but it does not work as needed Here is some content from csv file
id,location_id,name,title,email,department
1,1,Susan houston,Director of Services,,
2,1,Christina Gonzalez,Director,,
3,2,Brenda brown,"Director, Second Career Services",,
4,3,Howard Lader,"Manager, Senior Counseling",,
5,4,Kimberly Pesavento,Commercial director,,
6,5,Joe Bloom,Financial Empowerment Programs Program Director,,
7,6,peter Olson,Director,,
8,6,Bart charlow,Executive Director,,
9,7,Bart Charlow,Executive Director,,
#!/bin/bash
#create path to redirect accounts.csv to same directory as accounts_new.csv
path=$(dirname $1)
# Substituted commmas with vertical lines, so sed command works
awk -F'"' -v OFS='"' '{ for (i=2; i<=NF; i+=2) gsub(",", "|", $i) } 1' accounts.csv |
# Changed first letters of names to uppercase
awk -F, -v col=3 '
NR > 1{
n=split(tolower($col),a," ")
$col=toupper(substr(a[1],1,1)) substr(a[1],2)
for(i=2;i<=n;i++) {
$col=$col " " toupper(substr(a[i],1,1)) substr(a[i],2)
}
}1' OFS="," |
# Generated email addresses
sed -E 's/([^,]*,([^,]*),) ?(([[:alpha:]])[^ ]* +)(([^,]*),[^,]*,)[^,]*/\1\u\3\u\5\L\4\6\@abc.com/' |
awk -F, '{for (i=5;i<=5;i++){if (v[i,$i]++){b[$i]=$i;
$i=split($3,arr," ")
val=(substr($3,1,1) arr[2]$2"@abc.com")
$5=tolower(val)
}};print $0}' OFS="," |
# Added missing commas and sent output to new file
sed -E 's/\|/\,/g' > $path"/accounts_new.csv"
here is the output of the script
id,location_id,name,title,email,department
1,1,Susan Houston,Director of Services,shouston@abc.com,
2,1,Christina Gonzalez,Director,cgonzalez@abc.com,
3,2,Brenda Brown,Director, Second Career Services,bbrown@abc.com,
4,3,Howard Lader,Manager, Senior Counseling,hlader@abc.com,
5,4,Kimberly Pesavento,Commercial director,kpesavento@abc.com,
6,5,Joe Bloom,Financial Empowerment Programs Program Director,jbloom@abc.com,
7,6,Peter Olson,Director,polson@abc.com,
8,6,Bart Charlow,Executive Director,bcharlow@abc.com,
9,7,Bart Charlow,Executive Director,bcharlow7@abc.com,
but desired output is this
id,location_id,name,title,email,department
1,1,Susan Houston,Director of Services,shouston@abc.com,
2,1,Christina Gonzalez,Director,cgonzalez@abc.com,
3,2,Brenda Brown,Director, Second Career Services,bbrown@abc.com,
4,3,Howard Lader,Manager, Senior Counseling,hlader@abc.com,
5,4,Kimberly Pesavento,Commercial director,kpesavento@abc.com,
6,5,Joe Bloom,Financial Empowerment Programs Program Director,jbloom@abc.com,
7,6,Peter Olson,Director,polson@abc.com,
8,6,Bart Charlow,Executive Director,bcharlow6@abc.com,
9,7,Bart Charlow,Executive Director,bcharlow7@abc.com,
As you can see from last 2 rows location_id is appended only the second equal email but the first one remains untouched, how can I add location_id to the first one also?
Can you help me? Thanks!
I tried to use while instead of if statement but it didn't help me