简体   繁体   English

bash-用sed替换字符串

[英]bash - replacing string with sed

For some mysterious reason, some elements in my CSV data appear as s/stWgvN52??f2& ?" 由于某些神秘的原因,我的CSV数据中的某些元素显示为s/stWgvN52??f2& ?" s/stWgvN52??f2& ?" instead of stWgvN522tw0JtZZnyXj , which messes up the file because I have ; 而不是stWgvN522tw0JtZZnyXj ,因为我有,它弄乱了文件; set as the CSV delimiter. 设置为CSV分隔符。

I attempted to replace the defective string using sed as follows: 我尝试使用sed替换有缺陷的字符串,如下所示:

$ sed -i 's/stWgvN52??f2& ?"/stWgvN522tw0JtZZnyXj/g' file.csv

but I get the following error: sed: 1: "access_logs_2014-04.csv": command a expects \\ followed by text 但出现以下错误:sed:1:“ access_logs_2014-04.csv”:命令a期望\\后跟文本

What is the reason? 是什么原因?

When you use the -i option, you have to specify the extension of the backup file that gets made. 使用-i选项时,必须指定获取的备份文件的扩展名。 Some versions of sed expect the extension directly appended to the -i option, so what you wrote would work. 某些版本的sed期望将扩展名直接附加到-i选项,因此您编写的内容将起作用。 But other versions (like the version on OS X) require it to be a separate option, so you have to write: 但是其他版本(例如OS X上的版本)要求将其作为单独的选项,因此您必须编写:

sed -i '' 's/stWgvN52??f2& ?"/stWgvN522tw0JtZZnyXj/g' file.csv

to specify that you don't want a backup file. 指定您不需要备份文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM