Linux乱码转换(一)

带有中文的文件传到Linux服务器,打开后,看到的全是windows乱码。下面介绍如何将在Linux环境下打开中文字符文件,而不出现乱码。
1)载window下查看文件: Hadoop权威指南.txt

在这本《Hadoop权威指南(第3版)》即将出版之际,我十分高兴地再次向广大读者推荐这本书。
  一本书的价值最重要的是取决于它的用途。今天,开源的Hadoop已经成为研究大数据十分重要的平台,
在我国已经形成一个庞大的Hadoop用户社群,他们对学习、掌握和提高Hadoop提出了很高的需求,
《Hadoop权威指南(第3版)》恰好满足这样的需要,其用途和价值不言而喻。这一点也可以从下面的事实中得到佐证:
从2011年年底出版至今,本书的第2版已经实现了12次印刷,拥有近3.5万名读者并连续两年位列专业图书畅销榜榜首。
  一本书的价值还来自于它的内容。原著是用英文写作的,它的内容组织得当,思路清晰,紧密结合实际。
但是要把它翻译成中文介绍给中国的读者,并非易事。它不单单要求译者能够熟练地掌握英文,还要求他们对
书中的技术性内容有深入、准确的了解和掌握。从这两点来审视,本书的译者团队完全足以胜任。作为大学老师,
他们不仅在大数据领域从事一线教学和科研,同时还负责指导研究生从事数据库方面的研究。从 2006 年开始,
他们就在计算机集群上部署了Hadoop并成功完成了很多项研究。在这几年的工作过程中,他们对Hadoop及其应用
开发有着深入的理解和认识,这是本书翻译质量有保证的重要前提。
  经过再一次升级、修订和更新,值此《Hadoop权威指南(第3版)》出版之际,我衷心地希望这本书继续为广
大读者带来更高的学习价值,更友好的阅读体验。



2)强中文件字符文件  AA乱码.txt 上传至Linux服务器,放在 /root/demo下。

3)#cat  Hadoop权威指南.txt 
??????Hadoop??????(??3??)??????????????????????????????????????????顣
?????????????????????????????????????????????Hadoop???????о???????????????????
?????????γ?????????Hadoop??????????????????????????Hadoop????????????
??Hadoop??????(??3??)???????????????????????????????????????????????????????????е???????
??2011??????????????????????2??????????12????????н?3.5?????????????????λ??????鳩?????????
????????????????????????????????????????д??????????????????????·?????????????????
????????????????????????й?????????????????????????????????????????????????????????
???е???????????????????????????????????????????????????????????????????Ρ????????????
???????????????????????????????У?????????????о?????????????????о????? 2006 ?????
??????????????????????Hadoop????????????????о???????????????????У??????Hadoop??????
???????????????????????????????鷭???????б??????????
????????????????????????????????Hadoop??????(??3??)??????????????????????????????
?????????????????????????????????顣

4)查看文件编码类型 file  Hadoop权威指南.txt  
#file Hadoop权威指南.txt
Hadoop权威指南.txt: ISO-8859 text, with CRLF line terminators

5)查看系统字符集设置
# echo $LANG
en_US.UTF-8
编码为UTF-8
6)下面就将  Hadoop权威指南.txt  转换为  UTF-8

由第 4)步看到文件的类型为 ISO-8859,其实在windows中文字符实际为GBK

所以可以进行如下转换:
# iconv -f GBK -t UTF-8 Hadoop权威指南.txt -o Hadoop权威指南_UTF8.txt
iconv: 无法打开输入文件“Hadoop权威指南.txt”: 没有那个文件或目录
报错
以上报错说明iconv无法识别文件名
我们还需要把文件名转换一下。
可以使用工具convmv
执行如下:
# convmv -f GBK -t UTF-8 --notest Hadoop权威指南.txt
Skipping, already UTF-8: ./Hadoop权威指南.txt
Ready!
然后再执行如上命令文件内容转换命令
# iconv -f GBK -t UTF-8 Hadoop权威指南.txt -o Hadoop权威指南_UTF8.txt  ##无结果回显
查看转码后的文件内容,如下:

# cat Hadoop权威指南_UTF8.txt
在这本《Hadoop权威指南(第3版)》即将出版之际,我十分高兴地再次向广大读者推荐这本书。
  一本书的价值最重要的是取决于它的用途。今天,开源的Hadoop已经成为研究大数据十分重要的平台,
在我国已经形成一个庞大的Hadoop用户社群,他们对学习、掌握和提高Hadoop提出了很高的需求,
《Hadoop权威指南(第3版)》恰好满足这样的需要,其用途和价值不言而喻。这一点也可以从下面的事实中得到佐证:
从2011年年底出版至今,本书的第2版已经实现了12次印刷,拥有近3.5万名读者并连续两年位列专业图书畅销榜榜首。
  一本书的价值还来自于它的内容。原著是用英文写作的,它的内容组织得当,思路清晰,紧密结合实际。
但是要把它翻译成中文介绍给中国的读者,并非易事。它不单单要求译者能够熟练地掌握英文,还要求他们对
书中的技术性内容有深入、准确的了解和掌握。从这两点来审视,本书的译者团队完全足以胜任。作为大学老师,
他们不仅在大数据领域从事一线教学和科研,同时还负责指导研究生从事数据库方面的研究。从 2006 年开始,
他们就在计算机集群上部署了Hadoop并成功完成了很多项研究。在这几年的工作过程中,他们对Hadoop及其应用
开发有着深入的理解和认识,这是本书翻译质量有保证的重要前提。
  经过再一次升级、修订和更新,值此《Hadoop权威指南(第3版)》出版之际,我衷心地希望这本书继续为广
大读者带来更高的学习价值,更友好的阅读体验。


以上即为在linux环境下中文内容文件的显示。














来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9606353/viewspace-1975971/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/9606353/viewspace-1975971/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
NAME SYNOPSIS OPTIONS DESCRIPTION Filesystem issues HFS+ on OS X / Darwin JFS NFS4 FAT/VFAT and NTFS How to undo double UTF-8 (or other) encoded filenames How to repair Samba files Netatalk interoperability issues SEE ALSO BUGS AUTHOR NAME convmv - converts filenames from one encoding to another SYNOPSIS convmv [options] FILE(S) ... DIRECTORY(S) OPTIONS -f ENCODING specify the current encoding of the filename(s) from which should be converted -t ENCODING specify the encoding to which the filename(s) should be converted -i interactive mode (ask y/n for each action) -r recursively go through directories --nfc target files will be normalization form C for UTF-8 (Linux etc.) --nfd target files will be normalization form D for UTF-8 (OS X etc.). --qfrom , --qto be more quiet about the "from" or "to" of a rename (if it screws up your terminal e.g.). This will in fact do nothing else than replace any non-ASCII character (bytewise) with ? and any control character with * on printout, this does not affect rename operation itself. --exec command execute the given command. You have to quote the command and #1 will be substituted by the old, #2 by the new filename. Using this option link targets will stay untouched. Example: convmv -f latin1 -t utf-8 -r --exec "echo #1 should be renamed to #2" path/to/files --list list all available encodings. To get support for more Chinese or Japanese encodings install the Perl HanExtra or JIS2K Encode packages. --lowmem keep memory footprint low by not creating a hash of all files. This disables checking if symlink targets are in subtree. Symlink target pointers will be converted regardlessly. If you convert multiple hundredthousands or millions of files the memory usage of convmv might grow quite high. This option would help you out in that case. --nosmart by default convmv will detect if a filename is already UTF8 encoded and will skip this file if conversion from some charset to UTF8 should be performed. --nosmart will also force conversion to UTF-8 for such files, which might result in "double encoded UTF-8" (see section below). --fixdouble using the --fixdouble option convmv does only convert files which will still be UTF-8 encoded after conversion. That's useful for fixing double-encoded UTF-8 files. All files which are not UTF-8 or will not result in UTF-8 after conversion will not be touched. Also see chapter "How to undo double UTF-8 ..." below. --notest Needed to actually rename the files. By default convmv will just print what it wants to do. --parsable This is an advanced option that people who want to write a GUI front end will find useful (some others maybe, too). It will convmv make print out what it would do in an easy parsable way. The first column contains the action or some kind of information, the second column mostly contains the file that is to be modified and if appropriate the third column contains the modified value. Each column is separated by \0\n (nullbyte newline). Each row (one action) is separated by \0\0\n (nullbyte nullbyte newline). --preserve-mtimes modifying filenames usually causes the parent directory's mtime being updated. This option allows to reset the mtime to the old value. If your filesystem supports sub-second resolution the sub-second part of the atime and mtime will be lost as Perl does not yet support that. --replace if the file to which shall be renamed already exists, it will be overwritten if the other file content is equal. --unescape this option will remove this ugly % hex sequences from filenames and turn them into (hopefully) nicer 8-bit characters. After --unescape you might want to do a charset conversion. This sequences like etc. are sometimes produced when downloading via http or ftp. --upper , --lower turn filenames into all upper or all lower case. When the file is not ASCII-encoded, convmv expects a charset to be entered via the -f switch. --dotlessi care about the dotless i/I issue. A lowercase version of "I" will also be dotless while an uppercase version of "i" will also be dotted. This is an issue for Turkish and Azeri. By the way: The superscript dot of the letter i was added in the Middle Ages to distinguish the letter (in manuscripts) from adjacent vertical strokes in such letters as u, m, and n. J is a variant form of i which emerged at this time and subsequently became a separate letter. --help print a short summary of available options --dump-options print a list of all available options DESCRIPTION convmv is meant to help convert a single filename, a directory tree and the contained files or a whole filesystem into a different encoding. It just converts the filenames, not the content of the files. A special feature of convmv is that it also takes care of symlinks, also converts the symlink target pointer in case the symlink target is being converted, too. All this comes in very handy when one wants to switch over from old 8-bit locales to UTF-8 locales. It is also possible to convert directories to UTF-8 which are already partly UTF-8 encoded. convmv is able to detect if certain files are UTF-8 encoded and will skip them by default. To turn this smartness off use the --nosmart switch. Filesystem issues Almost all POSIX filesystems do not care about how filenames are encoded, here are some exceptions: HFS+ on OS X / Darwin Linux and (most?) other Unix-like operating systems use the so called normalization form C (NFC) for its UTF-8 encoding by default but do not enforce this. Darwin, the base of the Macintosh OS enforces normalization form D (NFD), where a few characters are encoded in a different way. On OS X it's not possible to create NFC UTF-8 filenames because this is prevented at filesystem layer. On HFS+ filenames are internally stored in UTF-16 and when converted back to UTF-8, for the underlying BSD system to be handable, NFD is created. See http://developer.apple.com/qa/qa2001/qa1173.html for defails. I think it was a very bad idea and breaks many things under OS X which expect a normal POSIX conforming system. Anywhere else convmv is able to convert files from NFC to NFD or vice versa which makes interoperability with such systems a lot easier. JFS If people mount JFS partitions with iocharset=utf8, there is a similar problem, because JFS is designed to store filenames internally in UTF-16, too; that is because Linux' JFS is really JFS2, which was a rewrite of JFS for OS/2. JFS partitions should always be mounted with iocharset=iso8859-1, which is also the default with recent 2.6.6 kernels. If this is not done, JFS does not behave like a POSIX filesystem and it might happen that certain files cannot be created at all, for example filenames in ISO-8859-1 encoding. Only when interoperation with OS/2 is needed iocharset should be set according to your used locale charmap. NFS4 Despite other POSIX filesystems RFC3530 (NFS 4) mandates UTF-8 but also says: "The nfs4_cs_prep profile does not specify a normalization form. A later revision of this specification may specify a particular normalization form." In other words, if you want to use NFS4 you might find the conversion and normalization features of convmv quite useful. FAT/VFAT and NTFS NTFS and VFAT (for long filenames) use UTF-16 internally to store filenames. You should not need to convert filenames if you mount one of those filesystems. Use appropriate mount options instead! How to undo double UTF-8 (or other) encoded filenames Sometimes it might happen that you "double-encoded" certain filenames, for example the file names already were UTF-8 encoded and you accidently did another conversion from some charset to UTF-8. You can simply undo that by converting that the other way round. The from-charset has to be UTF-8 and the to-charset has to be the from-charset you previously accidently used. If you use the --fixdouble option convmv will make sure that only files will be processed that will still be UTF-8 encoded after conversion and it will leave non-UTF-8 files untouched. You should check to get the correct results by doing the conversion without --notest before, also the --qfrom option might be helpful, because the double utf-8 file names might screw up your terminal if they are being printed - they often contain control sequences which do funny things with your terminal window. If you are not sure about the charset which was accidently converted from, using --qfrom is a good way to fiddle out the required encoding without destroying the file names finally. How to repair Samba files When in the smb.conf (of Samba 2.x) there hasn't been set a correct "character set" variable, files which are created from Win* clients are being created in the client's codepage, e.g. cp850 for western european languages. As a result of that the files which contain non-ASCII characters are screwed up if you "ls" them on the Unix server. If you change the "character set" variable afterwards to iso8859-1, newly created files are okay, but the old files are still screwed up in the Windows encoding. In this case convmv can also be used to convert the old Samba-shared files from cp850 to iso8859-1. By the way: Samba 3.x finally maps to UTF-8 filenames by default, so also when you migrate from Samba 2 to Samba 3 you might have to convert your file names. Netatalk interoperability issues When Netatalk is being switched to UTF-8 which is supported in version 2 then it is NOT sufficient to rename the file names. There needs to be done more. See http://netatalk.sourceforge.net/2.0/htmldocs/upgrade.html#volumes-and-filenames and the uniconv utility of Netatalk for details. SEE ALSO locale(1) utf-8(7) charsets(7) BUGS no bugs or fleas known AUTHOR Bjoern JACKE Send mail to bjoern [at] j3e.de for bug reports and suggestions.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值