2013-01-19 49 views
1

我有一个csv文件,我想附加所有电子邮件.example.com。结果将是[email protected] 我想从终端执行这个ussing sed命令,但我无法弄清楚什么是正确的语法。 例子:Sed命令附加电子邮件地址

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

应该成为

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

我试着用sed -i "" -e 's/\(@[A-Z0-9.-]+\.[A-Z]{2,4}\)/\1.example.com/g' FILE.CSV,但它不工作

+0

所有电子邮件是相同还是独一无二的? – PassKit

+0

你的输入格式是什么?每行都是电子邮件地址?电子邮件地址是否在特定列中?每行是否需要修改? –

+0

这行是这样的:[email protected]; bla bla; – Andrei

回答

0

我刚刚注意到标签,这意味着你使用BSD sed。你需要的-E标志,使+工作:

$ 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 

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

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

+0

我已经设法解决这个问题:sed -i“.bkp”-E“s /(@ [a-zA-Z0-9 .-] +)/&。example.com/g”文件 – Andrei

+1

如果你'重新在替换中使用'&',在匹配中不需要括号:-) – cmbuckley

0

我想我找到了解决方案: SED -i “.bkp” -E“S /(@ [A-ZA-Z0-9 .-] +)/ &。实施例.COM/G” FILE.CSV

+0

括号是多余的 –

1

你可以只插入文本中的第一个分号前面:

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

它不会将name1;改为name1.example.com; 't要 – user2134226

+0

不,因为我没有使用'g'标志,所以它只会改变第一个分号 –

相关问题