如何使用 Awk 读取不同编码的文件? [英] How to read files with different encodings using Awk?

查看:35
本文介绍了如何使用 Awk 读取不同编码的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Awk 中正确读取 UTF8 以外的编码文件?

How can I correctly read files in encodings other than UTF8 in Awk?

我有一个希伯来语/Windows-1255 编码的文件.一个简单的 {print $0} awk 会打印像 之类的东西.我怎样才能让它正确读取?

I have a file in Hebrew/Windows-1255 encoding. A simple {print $0} awk prints stuff like �. how can I make it read correctly?

推荐答案

awk 本身不支持处理不同的编码.它将遵守环境中指定的语言环境,但最好的办法是将输入转码为正确的编码,然后再将其交给 awk.

awk itself doesn't have any support for handling different encodings. It will honor the locale specified in the environment, but your best bet is to transcode the input to the proper encoding before handing it off to awk.

-f 是您要转换的格式,-t 是目标格式,-c 跳过任何过早终止 iconv 操作的无效字符.当然 --help 会提供更多细节.

-f is the format you want to convert from, -t is the target format, and -c skips over any invalid characters which prematurely terminate iconv's operation. Of course --help will give more details.

iconv -c -f cp1255 -t utf8 somefile | awk ...

这篇关于如何使用 Awk 读取不同编码的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆