1

I've just joined SO after watching from the outside for just over a year now. The reason I've finally joined is because I could do with some help :)

I have a text file with a list of email addresses. The email addresses are in the following format:

<firstinitial><surname>@domain.com

I'd like to edit the text file so the output gives me:

<firstinitial><first3lettersofsurname>@domain.com

I've tried using sed, but can seem to get this one. Any help would be much appreciated.

Many thanks,

Phill.

Phr33fall
  • 11
  • 1
  • `tried using sed` please add that code to question.. as you've used SO before, you'd have seen questions adding code attempted by OP :) – Sundeep Mar 05 '18 at 15:34
  • and please add exact input/output sample without any modification.. for ex: it is not clear if you want to change `surname` to `first3lettersofsurname` or change `foobar` to `foo` – Sundeep Mar 05 '18 at 15:36
  • Thanks for the pointers Sundeep :) – Phr33fall Mar 05 '18 at 15:47

1 Answers1

1

With GNU sed:

sed -E 's/(....).*(@.*)/\1\2/' file

.: matches a single character. Does not matter what character it is, except newline

*: matches preceding match zero or more times

\1: repeat the first capturing group (....)in the matched expression

\2: repeat the second capturing group (@.*)in the matched expression


See: The Stack Overflow Regular Expressions FAQ

Community
  • 1
  • 1
Cyrus
  • 69,405
  • 13
  • 65
  • 117
  • Thank you Cyrus, this is awesome! :D Would you please be as kind to explain the syntax? It's worked a treat, but I'd like to understand exactly how it's worked. – Phr33fall Mar 05 '18 at 15:51
  • Thanks for taking the time to explain Cyrus, it's most appreciated. – Phr33fall Mar 08 '18 at 09:35