osx 更改文件编码(iconv)递归
我知道我可以使用以下命令在 OSX 下转换单个文件编码:
iconv -f ISO-8859-1 -t UTF-8 myfilename.xxx > myfilename-utf8.xxx
我必须转换一堆具有特定扩展名的文件, 所以我想将文件编码从 ISO-8859-1 转换为 UTF-8 对于文件夹 /mydisk/myfolder 中的所有 *.ext 文件
,也许有人知道语法如何执行此操作,
谢谢
ekke
I know I can convert a single file encoding under OSX using:
iconv -f ISO-8859-1 -t UTF-8 myfilename.xxx > myfilename-utf8.xxx
I have to convert a bunch of files with a specific extension,
so I want to convert file encoding from ISO-8859-1 to UTF-8
for all *.ext files in folder /mydisk/myfolder
perhaps someobe know the syntax how to do this
thanks
ekke
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
亚当的评论向我展示了解决问题的方法,
但这是我让它起作用的唯一语法:
-i ... -o ... 不起作用,但 > >
再次感谢
ekke
Adam' comment showed me the way how to resolve it,
but this was the only syntax I made it work:
-i ... -o ... doesnt work, but >
thx again
ekke
如果你的 shell 是 bash,类似这样
if your shell is bash, something like this
这是在 mac 10.10 中测试的示例。
按名称查找文件,转换编码,然后替换原始文件。工作完美。
感谢 Roman Truba 的示例,将下面的完整代码复制到您的 shell 脚本中。
Here is example Tested in mac 10.10.
Find file by name,convert encode ,then replace original file.work perfect.
Thanks for Roman Truba's example,COPY the full code below to your shell script.
试试这个......它经过测试并工作:
第一步(ICONV):
查找 /var/www/ -name *.php -type f | (读取文件时;执行 iconv -f ISO-8859-2 -t UTF-8 "$file" > "${file%.php}.phpnew";完成)
第二步(重写 - MV):
查找 /var/www/ -name "*.phpnew" -type f | (读取文件时;执行 mv $file
echo $file | sed 's/\(.*\.\)phpnew/\1php/'
;完成)这只是我研究的结论: )
希望能帮助到你
雅库布·鲁莱克
try this ... it´s tested and workin:
First step (ICONV):
find /var/www/ -name *.php -type f | (while read file; do iconv -f ISO-8859-2 -t UTF-8 "$file" > "${file%.php}.phpnew"; done)
Second step (REWRITE - MV):
find /var/www/ -name "*.phpnew" -type f | (while read file; do mv $file
echo $file | sed 's/\(.*\.\)phpnew/\1php/'
; done)It´s just conclusion on my research :)
Hope it helps
Jakub Rulec
我扩展了 Albert.Qings 脚本:
添加了目录和文件名模式的参数
<前><代码>#!/bin/bash
命令=${1-"用法"}
searchPattern=${2-"*.java"}
搜索目录=${3-"."}
if [[ "$command" == "用法" ]]; 然后
echo "将文件转换为utf8.sh [usage|dry|exec] [searchPattern=$searchPattern] [searchDirectory=$searchDirectory]"
出口
菲
查找 $searchDirectory -type f -name "$searchPattern" | \
(读取文件时;
if [[ "$file" != *.DS_Store* ]]; 执行此操作 然后
if [[ "$file" != *-utf8* ]]; 然后
currentEncoding="$(文件 --brief --mime-encoding $file)"
if [[ "$currentEncoding" != "utf-8" ]]; 然后
echo“命令:$命令/ iconv -f $currentEncoding -t UTF-8 $文件”
if [[ "$command" == "exec" ]]; 然后
iconv -f $currentEncoding -t UTF-8 "$file" > “$文件-utf8”;
rm $文件;
echo mv "$file-utf8" "$file";
mv "$file-utf8" "$file";
菲
菲
菲
菲
完毕);
已在 MacOS X 10.12.6 / Sierra 上测试。
I extended Albert.Qings script:
added a parameter for the directory and filename pattern
Tested on MacOS X 10.12.6 / Sierra.
您可以使用任何脚本语言编写脚本来迭代 /mydisk/myfolder 中的每个文件,使用正则表达式 [.(.*)$] 检查扩展名,如果它是“ext”,则运行以下命令(或等效命令):系统调用。
“ iconv -f ISO-8859-1 -t UTF-8” + file.getName() + “>” + file.getName() + "-utf8.xxx"
这在 Python 中只是几行,但我把它作为练习留给读者,让他们了解查找目录迭代和正则表达式的细节。
You could write a script in any scripting language to iterate over every file in /mydisk/myfolder, check the extension with the regex [.(.*)$], and if it's "ext", run the following (or equivalent) from a system call.
"iconv -f ISO-8859-1 -t UTF-8" + file.getName() + ">" + file.getName() + "-utf8.xxx"
This would only be a few lines in Python, but I leave it as an exercise to the reader to go through the specifics of looking up directory iteration and regular expressions.
如果你想递归地执行,可以使用
find(1)
:请注意,我使用了
| while read
而不是 find 的-exec
选项(或通过管道传输到xargs
),因为我们需要对文件名进行操作,即砍掉.xxx
扩展名(使用${file%.xxx}
)并添加-utf8.xxx
。If you want to do it recursively, you can use
find(1)
:Note that I've used
| while read
instead of the-exec
option of find (or piping intoxargs
) because of the manipulations we need to do with the filename, namely, chopping off the.xxx
extension (using${file%.xxx}
) and adding-utf8.xxx
.