使用shell脚本,我试图替换字符串的特定部分。
以下是文件及其内容的摘录:
"filename":"screenreader"}', '{"registration_email":"toto@toto.com","read_page":"1"
"unique_pagination":"1","registration_email":"toto@toto.com","searchbox_enable":"0",
"filename":"jspeed"}', '{"registration_email":"toto@toto.com","combine_files_enable":"1"
我想更换所有toto@toto.com另一个字符串。但是字符串toto@toto.com在系统上不会相同。。。可以tata@tata.com或其他什么。
这是我在shell脚本中的SED命令:
sed -i -e 's/\"registration_email\":\".*\"/\"registration_email\":\"TATATATATA\"/g' file.txt
该命令有效,字符串被正确替换,但每行的末尾都被删除,结果如下:
"filename":"screenreader"}', '{"registration_email":"TATATATATA"
"unique_pagination":"1","registration_email":"TATATATATA",
"filename":"jspeed"}', '{"registration_email":"TATATATATA"
预期结果应如下:
"filename":"screenreader"}', '{"registration_email":"TATATATATA","read_page":"1"
"unique_pagination":"1","registration_email":"TATATATATA","searchbox_enable":"0",
"filename":"jspeed"}', '{"registration_email":"TATATATATA","combine_files_enable":"1"
那么,如何在不删除每行末尾的情况下替换字符串呢?
感谢
使用-r启用正则表达式,并将行拆分为三个部分。将行替换为第一节和第二节,然后是替换电子邮件和第四节
sed -rn 's/(^.*)(registration_email\":\")(.*@.*)(\",\".*$)/12TATATATATA4/p' file
提交到文件
sed -ri 's/(^.*)(registration_email\":\")(.*@.*)(\",\".*$)/12TATATATATA4/' file