bash Sed 和 UTF-8 编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27072558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sed and UTF-8 encoding
提问by amorino
In SSH (Putty) I'm using a command to replace some text in a file which is encoded in UTF-8
在 SSH (Putty) 中,我使用命令替换以 UTF-8 编码的文件中的一些文本
For example
例如
cd ~/public_html/app/Config; sed -i 's/ADDRESS_SSH/l'adresse à été correctement intégréé/g' param.php;
cd ~/public_html/app/Config; sed -i 's/ADDRESS_SSH/l'adresse à été correctement intégréé/g' param.php;
In the file all is OK but when displaying in the browser the accents are strange
When verifing the file param.php I found that it's encoding was changing from UTF-8
to Europe de L'Ouest
在文件中一切正常,但是在浏览器中显示时,重音很奇怪在验证文件 param.php 时,我发现它的编码从 更改UTF-8
为Europe de L'Ouest
Same problem for .xml file too
.xml 文件也有同样的问题
Any help to use sed without changing the encoding or maybe to add an other ssh line for rencoding the php and xml files newly in UTF-8 ?
在不更改编码的情况下使用 sed 或添加其他 ssh 行以在 UTF-8 中重新编码新的 php 和 xml 文件是否有任何帮助?
Best regards and thank you
最好的问候和谢谢
采纳答案by amorino
It was on Putty configuration ==> Translation ==> Received data assumed to be in which character set ==> Choose UTF-8 Best regards
这是在腻子配置 ==> 翻译 ==> 接收的数据假定在哪个字符集 ==> 选择 UTF-8 最好的问候
回答by that other guy
You have to ensure that the issued sed
command is UTF-8
encoded.
您必须确保发出的sed
命令已UTF-8
编码。
You can do this in one of two ways:
您可以通过以下两种方式之一执行此操作:
Write the sed command to a file, ensure the file is UTF-8, and execute it as a script:
file yourfile
should sayUTF-8 Unicode text
.You can then run
bash yourfile
.Alternatively, change your terminal and shell settings to UTF-8
printf à | wc -c
must say 2, not 1.locale
should list "UTF-8" or "utf8" in theLC_CTYPE
line.You can then run the sed command straight from the terminal prompt.
将 sed 命令写入文件,确保文件为 UTF-8,并作为脚本执行:
file yourfile
应该说UTF-8 Unicode text
。然后就可以运行了
bash yourfile
。或者,将终端和外壳设置更改为 UTF-8
printf à | wc -c
必须说2,而不是1。locale
应该LC_CTYPE
在行中列出“UTF-8”或“utf8” 。然后,您可以直接从终端提示符运行 sed 命令。