Andrei
Andrei

Reputation: 302

Sed command append email address

I have a csv file and I want to append all emails with .example.com. The result will be [email protected] I want to do this ussing sed command from terminal but I can't figure out what is the correct syntax. Example:

[email protected];name1;surname1
[email protected];name2;surname2
[email protected];name3;surname3

Should become

[email protected];name1;surname1
[email protected];name2;surname2
[email protected];name3;surname3

I tried with sed -i "" -e 's/\(@[A-Z0-9.-]+\.[A-Z]{2,4}\)/\1.example.com/g' file.csv but it isn't working

Upvotes: 1

Views: 426

Answers (4)

Mirage
Mirage

Reputation: 31568

Try this

sed -re 's/(\w+@\w+.\w+)/\11.example.com/g' temp.txt

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 247012

You could just insert the text in front of the first semicolon:

sed 's/;/.example.com;/'

Upvotes: 1

Andrei
Andrei

Reputation: 302

I think I found the solution: sed -i ".bkp" -E "s/(@[a-zA-Z0-9.-]+)/&.example.com/g" file.csv

Upvotes: 0

cmbuckley
cmbuckley

Reputation: 42507

I've just noticed the tag, which means you're using BSD sed. You'll need the -E flag to make the + work:

$ cat > file

[email protected];name1;surname1
[email protected];name2;surname2
[email protected];name3;surname3

$ sed -E 's/(@[a-zA-Z0-9.-]+)/\1.example.com/' file

[email protected];name1;surname1
[email protected];name2;surname2
[email protected];name3;surname3

Your character class also needs to check for a-z as well as A-Z, since they are case-sensitive by default.

Reference: How to escape plus sign on mac os x (BSD) sed?

Upvotes: 1

Related Questions