0

I have a CSV file that looks like this:

English, "Hawkin, Jason" ,[email protected]
English, "McDonald, Matt" ,[email protected]
English, "Campbell, Josh" ,[email protected]

My intention is to make a Bash Script to make a second CSV file with the following format:

ID, Email, FNAME, LNAME
jhawkin110, [email protected], Jason, Hawkin

After using "cut" to remove the first column I did not need, I am at a loss on how to create the "ID" by extracting the first part of the "Email".

3
  • Forget the cut and check out awk. Commented Dec 4, 2021 at 13:39
  • Please add to your question (no comment): What have you searched for, and what did you find? What have you tried, and how did it fail? Commented Dec 4, 2021 at 13:44
  • What's your field separator? Comma, comma followed by one space or one space followed by comma? Commented Dec 4, 2021 at 13:46

1 Answer 1

1

Using awk

$ awk 'BEGIN {FS=OFS=","; print "ID, Email, FNAME, LNAME"}{gsub(/"/,"");split($NF,a,"@"); print tolower(a[1])," " $NF, $3, $2}' input_file
ID, Email, FNAME, LNAME
jhawkin110, [email protected], Jason , Hawkin
mmcdonald114, [email protected], Matt , McDonald
jcampbell111, [email protected], Josh , Campbell

Using sed

$ sed -E 's/.*"([^,]*),([^"]*)..,(([^@]*)@.*)/\3,\2, \1,\L\4/;s/(.*),(.*)/\2, \1/;1iID, Email, FNAME, LNAME' input_file
ID, Email, FNAME, LNAME
jhawkin110, [email protected], Jason, Hawkin
mmcdonald114, [email protected], Matt, McDonald
jcampbell111, [email protected], Josh, Campbell
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.