Sed命令附加电子邮件地址

时间:2013-01-19 13:43:40

标签: regex sed osx-lion

我有一个csv文件,我想用.example.com附加所有电子邮件。结果将是foo@bar.com.example.com 我想从终端执行这个ussing sed命令,但我无法弄清楚什么是正确的语法。 例如:

email1@foo.bar;name1;surname1
email3@foo.bar;name2;surname2
email3@foo.bar;name3;surname3

应该成为

email1@foo.bar.example.com;name1;surname1
email3@foo.bar.example.com;name2;surname2
email3@foo.bar.example.com;name3;surname3

我尝试使用sed -i "" -e 's/\(@[A-Z0-9.-]+\.[A-Z]{2,4}\)/\1.example.com/g' file.csv,但它无效

4 个答案:

答案 0 :(得分:1)

我刚刚注意到标签,这意味着你正在使用BSD sed。您需要-E标记才能使+工作:

$ cat > file

email1@foo.bar;name1;surname1
email3@foo.bar;name2;surname2
email3@foo.bar;name3;surname3

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

email1@foo.bar.example.com;name1;surname1
email3@foo.bar.example.com;name2;surname2
email3@foo.bar.example.com;name3;surname3

您的角色类还需要检查a-z以及A-Z,因为默认情况下它们区分大小写。

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

答案 1 :(得分:1)

您只需将文本插入第一个分号前面:

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

答案 2 :(得分:0)

我想我找到了解决方案: sed -i“.bkp”-E“s /(@ [a-zA-Z0-9 .-] +)/& .example.com / g”file.csv

答案 3 :(得分:0)

试试这个

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

相关问题