0

I have a list of my customers emails and want to remove some that end in .br for example. I would normally do the following command:

sed -i '/.br/d' customers.csv

But that would also delete a customers email that was something like xyz.br@gmail.com.

Example of a customer detail is:

"Phone Number","EMAIL@XYZ.com","NAME"

How would I delete only customers emails that end in .br?

  • 2
    Search for \.br" (or \.br",, if there's a risk that NAME might end with .br). – G-Man May 18 '15 at 18:55
0

You need to escape the . so that it does not match any character to make sure it will not match something like "name@add.abr" for example. You can also look for .br that appears after an @.

Try

 sed -i '/".*\@[^"]*\.br"/d' customer.csv

Here is an example run:

~$ echo '"Phone Number","EMAIL@XYZ.com","NAME"
> "Phone Number2","EMAIL@XYZ.br","NAME2"
> "Phone Number3","EMAIL@XYZ.abr","NAME3"
> "Phone Number","EMAIL@ABCbr.com","NAME"
> "Phone Number","EMAIL@XYZ.com","NAME.br"' > customers.csv

~$ cat customers.csv
"Phone Number","EMAIL@XYZ.com","NAME" 
"Phone Number2","EMAIL@XYZ.br","NAME2"  <-- should get deleted
"Phone Number3","EMAIL@XYZ.abr","NAME3"
"Phone Number","EMAIL@ABCbr.com","NAME"
"Phone Number","EMAIL@XYZ.com","NAME.br"

~$ sed -i '/".*@.*\.br"/d' customer.csv 

~$ cat customers.csv 
"Phone Number","EMAIL@XYZ.com","NAME"
"Phone Number3","EMAIL@XYZ.abr","NAME3"
"Phone Number","EMAIL@ABCbr.com","NAME"
"Phone Number","EMAIL@XYZ.com","NAME.br"
  • sed -i '/".*@.*\..br"/d' customers.csv did not work. – Teddy291 May 18 '15 at 19:36
  • sed -i '/@.*\.br"/d' customers.csv just deleted the whole file content. – Teddy291 May 18 '15 at 19:52
  • I updated the expression.. And showed example output. – Rob May 19 '15 at 13:27

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.