osx 更改文件编码(iconv)递归

发布于 2024-07-29 06:14:43 字数 267 浏览 10 评论 0原文

我知道我可以使用以下命令在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

时间你老了 2024-08-05 06:14:43

亚当的评论向我展示了解决问题的方法,
但这是我让它起作用的唯一语法:

find /mydisk/myfolder -name \*.xxx -type f | \
    (while read file; do
        iconv -f ISO-8859-1 -t UTF-8 "$file" > "${file%.xxx}-utf8.xxx";
    done);

-i ... -o ... 不起作用,但 > >

再次感谢

ekke

Adam' comment showed me the way how to resolve it,
but this was the only syntax I made it work:

find /mydisk/myfolder -name \*.xxx -type f | \
    (while read file; do
        iconv -f ISO-8859-1 -t UTF-8 "$file" > "${file%.xxx}-utf8.xxx";
    done);

-i ... -o ... doesnt work, but >

thx again

ekke

小镇女孩 2024-08-05 06:14:43

如果你的 shell 是 bash,类似这样

for files in /mydisk/myfolder/*.xxx
do
  iconv -f ISO-8859-1 -t UTF-8 "$files" "${files%.xxx}-utf8.xxx"
done

if your shell is bash, something like this

for files in /mydisk/myfolder/*.xxx
do
  iconv -f ISO-8859-1 -t UTF-8 "$files" "${files%.xxx}-utf8.xxx"
done
找回味觉 2024-08-05 06:14:43

这是在 mac 10.10 中测试的示例。
按名称查找文件,转换编码,然后替换原始文件。工作完美。
感谢 Roman Truba 的示例,将下面的完整代码复制到您的 shell 脚本中。

   #!/bin/bash
        find ./ -name *.java -type f | \
        (while read file;
            do if [[ "$file" != *.DS_Store* ]]; then
            if [[ "$file" != *-utf8* ]]; then
                iconv -f ISO-8859-1 -t UTF-8 "$file" > "$file-utf8";
                rm $file;
                echo mv "$file-utf8" "$file";
                mv "$file-utf8" "$file";
            fi
        fi 
        done);

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.

   #!/bin/bash
        find ./ -name *.java -type f | \
        (while read file;
            do if [[ "$file" != *.DS_Store* ]]; then
            if [[ "$file" != *-utf8* ]]; then
                iconv -f ISO-8859-1 -t UTF-8 "$file" > "$file-utf8";
                rm $file;
                echo mv "$file-utf8" "$file";
                mv "$file-utf8" "$file";
            fi
        fi 
        done);
寂寞笑我太脆弱 2024-08-05 06:14:43

试试这个......它经过测试并工作:

第一步(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

暗藏城府 2024-08-05 06:14:43

我扩展了 Albert.Qings 脚本:

  • 自动检测当前文件编码
  • 添加了一个命令参数来执行 dry/exec-run
  • 添加了目录和文件名模式的参数

    <前><代码>#!/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:

  • autodetect the current file encoding
  • added a command parameter to do a dry/exec-run
  • added a parameter for the directory and filename pattern

    #!/bin/bash
    command=${1-"usage"}
    searchPattern=${2-"*.java"}
    searchDirectory=${3-"."}
    if [[ "$command" == "usage" ]]; then
        echo "convert-file-to-utf8.sh [usage|dry|exec] [searchPattern=$searchPattern] [searchDirectory=$searchDirectory]"
        exit
    fi
    find $searchDirectory -type f -name "$searchPattern" | \
    (while read file;
        do if [[ "$file" != *.DS_Store* ]]; then
        if [[ "$file" != *-utf8* ]]; then
            currentEncoding="$(file --brief --mime-encoding $file)"
            if [[ "$currentEncoding" != "utf-8" ]]; then
               echo "command:$command / iconv -f $currentEncoding -t UTF-8 $file"
               if [[ "$command" == "exec" ]]; then
                 iconv -f $currentEncoding -t UTF-8 "$file" > "$file-utf8";
                 rm $file;
                 echo mv "$file-utf8" "$file";
                 mv "$file-utf8" "$file";
              fi
            fi
        fi
    fi
    done);
    

Tested on MacOS X 10.12.6 / Sierra.

や莫失莫忘 2024-08-05 06:14:43

您可以使用任何脚本语言编写脚本来迭代 /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.

静若繁花 2024-08-05 06:14:43

如果你想递归地执行,可以使用 find(1)

find /mydisk/myfolder -name \*.xxx -type f | \
    (while read file; do
        iconv -f ISO-8859-1 -t UTF-8 -i "$file" -o "${file%.xxx}-utf8.xxx
    done)

请注意,我使用了 | while read 而不是 find 的 -exec 选项(或通过管道传输到 xargs),因为我们需要对文件名进行操作,即砍掉.xxx 扩展名(使用 ${file%.xxx})并添加 -utf8.xxx

If you want to do it recursively, you can use find(1):

find /mydisk/myfolder -name \*.xxx -type f | \
    (while read file; do
        iconv -f ISO-8859-1 -t UTF-8 -i "$file" -o "${file%.xxx}-utf8.xxx
    done)

Note that I've used | while read instead of the -exec option of find (or piping into xargs) because of the manipulations we need to do with the filename, namely, chopping off the .xxx extension (using ${file%.xxx}) and adding -utf8.xxx.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文