-1

I have file data as below

  1. bill johnson
  2. robert dylan
  3. neil o'bryan

and the requirement is to extract the last name and add @mail.com to it. I used the below command

cut -d"|" -f2 emp.lst | sed 's/\([a-z][a-z]*\) \([a-z][a-z]*\)/\2@mail.com/'

-- Cut command to get the second field from the file (full name) -- Sed to separate first and last name and assign email to the last name

Everything works fine except the name neil o'bryan and below is the result I got

  1. johnson@mail.com
  2. dylan@gmail.com
  3. o@gmail.com'bryan

Please help me how to get the name with single quote work as expected. i.e. o'bryan@mail.com

  • 1
    awk '{printf "%s\n", $2"@mail.com"}' file (or $3 if the lines are actually numbered)... – jasonwryan Aug 28 '16 at 7:56
  • 2
    You are using cut -d"|" but your data file has no | characters in it. Explain. – John1024 Aug 28 '16 at 8:07
  • 1
    Do you think \([a-z][a-z]*\) could possible match o'bryan? :) – Satō Katsura Aug 28 '16 at 8:09
  • Its not a possible match. I am not sure how to include single quote here to match o'bryan. – AngiSen Aug 28 '16 at 8:45
0

Assuming that o'brian should be obrian in the email address, just add a tr stage to the pipeline to get rid of unwanted characters:

cut -d "|" -f2 emp.lst |
tr -d "'" |
sed 's/\([a-z][a-z]*\) \([a-z][a-z]*\)/\2@mail.com/'

You could do the same with any other characters you'd like to remove, or convert to something else, like

cut -d "|" -f2 emp.lst |
tr -d "'" |
tr 'åäöé' 'aaoe' |
sed 's/\([a-z][a-z]*\) \([a-z][a-z]*\)/\2@mail.com/'

If the address should be o'brian@mail.com (with the apostrophe in place), just add that to the regular expression:

cut -d "|" -f2 emp.lst |
sed "s/\([a-z][a-z]*\) \([a-z]['a-z]*\)/\2@mail.com/"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.