0
sed '/[A-Za-z0-9][A-Za-z0-9._%+-]+@[A-Za-z0-9][A-Za-z0-9.-]+\.[A-Za-z]{2,3}/d' emails.txt

is supposed to remove proper email addresses from emails.txt file. But it does not. Please help me...

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • 2
    `sed` uses "basic" regex syntax by default, use `sed -E` for "extended" syntax (see [this answer](https://stackoverflow.com/questions/6156259/sed-expression-doesnt-allow-optional-grouped-string/6157705#6157705)). Also, some valid TLDs are more than 3 letters long, and and the `.` before the TLD isn't escaped or bracketed, so it'll match any character (that's what `.` does in a regex), and finally your pattern isn't anchored, so it'll match if any part of the line is a valid email (e.g. "`@*()$#%*&*@example@gmail.com&)*%|@!|:;'" will match). – Gordon Davisson Mar 21 '21 at 08:04
  • 3
    See [How to validate an email address using a regular expression?](https://stackoverflow.com/q/201323/3422102) – David C. Rankin Mar 21 '21 at 08:45
  • sed seems to work ok for me, for the time being, but why it does not delete any emails? It lists the wrong ones in console only... – Wojciech Mierzejewski Mar 21 '21 at 12:37
  • Your sed command removes whole lines containing your pattern (that only works with POSIX ERE enabled). You need to use a substitution command to remove the emails only, `sed -E 's/[A-Za-z0-9][A-Za-z0-9._%+-]+@[A-Za-z0-9][A-Za-z0-9.-]*\.[A-Za-z]{2,3}//g' emails.txt` – Wiktor Stribiżew Mar 21 '21 at 13:36

0 Answers0