简体   繁体   English

在Linux中的文件中替换单引号和双引号

[英]replace single quote and double quote in files in linux

I took a dump from postgres which created a bunch of CSVs, which have single (') and double (") characters in a few huge files. 我从postgres进行了转储,它创建了一堆CSV,在一些大文件中具有单(')和双(“)字符。

How can I replace these with \\" and \\' in linux? 如何在Linux中将它们替换为\\“和\\'?

Thanks! 谢谢!

You can use sed : 您可以使用sed

sed "s/['\"]/\\\\&/g" <<< "abc'def',\"foo\",bar"

abc\'def\',\"foo\",bar

If you want to avoid escaping already escaped quotes then use: 如果要避免转义已经转义的引号,请使用:

sed -E "s/(^|[^\\])(['\"])/\\1\\\\\\2/g" file

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

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