I have two csv file. Both files might have same or different data. File2 has only few columns from file 1. Some column in file 2 may have different header. eg File 2 has Name in place of First Name
Username, Identifier,One-time password,Recovery code,First name,Last name,Department,Location
booker12,9012,12se74,rb9012,Rachel,Booker,Sales,Manchester
grey07,2070,04ap67,lg2070,Laura,Grey,Depot,London
johnson81,4081,30no86,cj4081,Craig,Johnson,Depot,London
jenkins46,9346,14ju73,mj9346,Mary,Jenkins,Engineering,Manchester
smith79,5079,09ja61,js5079,Jamie,Smith,Engineering,Manchester
File 2
Department,First name,Last name,One-time password
Sales,Rachel,Booker,12se74
Depot,Laura,Grey,04ap67
Depot,Craig,Johnson,30no86
Engineering,Mary,Jenkins,14ju73
Engineering,Jamie,Smith,09ja61
Problem 1: I wanted to reorder columns in csv file 2 to match order in csv file 1 based on headers.
Desired output header sorted based on file 1
One-time password,Name,Last name,Department
12se74,Dash,Bok,Sales
04ap67,Claire,Trans,Accounts
30no86,Shane,Walter,Depot
14ju73,Leon,Jenkins,Engineering
09ja61,Oliver,Den,Engineering
Problem 2: Merge File2 columns to File 1 based on header removing duplicates Eg if First Name , Last Name, Department columns are same then those are duplicates remove those dupes. Other columns may or may not be same. Hence wanting to achive condition based deduplication of record
Problem 3: Convert file 2 as file 1 template add missing columns in order. Finally compare and delete duplicate columns based on certain headers. eg. If First Name, Last Name and password are same then they are duplicates other columns may be same or different.
Problem 4: Copy specific column from file 2 to file 1 preserving order Eg. File 2 has Name column Replace that column with First Name column of file 1
Tried:
awk -v FS=, -v OFS=, 'FNR==NR{hash[FNR]=$5; next}{$2 = hash[FNR]}1' file file2
Above answer is from https://unix.stackexchange.com/questions/674038/replace-a-column-value-in-csv-file-from-another-file
Above seem to work. But it requires to specify column numbet as $5 and $2. Can anyone help modify above command to specify header instead of column number
Tried
awk -v FS=, -v OFS=, '{ for (i=1;i<=NF;i++) { if (i=="name") var=$i }; FNR==NR{hash[FNR]=$5; next}{$var = hash[FNR] }' file file2
Not working