第 11 章 資料轉換

目录

11.1. 文字資料轉換工具
11.1.1. 用 iconv 命令來轉換文字檔案
11.1.2. 用 iconv 檢查檔案是不是 UTF-8 編碼
11.1.3. Converting file names with iconv
11.1.4. EOL conversion
11.1.5. TAB 轉換
11.1.6. 帶有自動轉換功能的編輯器
11.1.7. Plain text extraction
11.1.8. 高亮並格式化純文字資料
11.2. XML 資料
11.2.1. Basic hints for XML
11.2.2. XML processing
11.2.3. The XML data extraction
11.3. 排版
11.3.1. roff 排版
11.3.2. TeX/LaTeX
11.3.3. 漂亮的列印手冊頁
11.3.4. 建立手冊頁
11.4. 可印刷的資料
11.4.1. Ghostscript
11.4.2. 合併兩個 PS 或 PDF 檔案
11.4.3. 處理可印刷資料的工具
11.4.4. 用 CUPS 列印
11.5. 郵件資料轉換
11.5.1. 郵件資料基礎
11.6. 圖形資料工具
11.7. 不同種類的資料轉換工具

下面是關於Debian 系統上可用的格式轉化工具及其相關提示的資訊。

Standard based tools are in very good shape but support for proprietary data formats are limited.

如下是文字資料轉換工具。


[提示] 提示

iconv(1)libc6 軟體包的一部分並且它可以在類 Unix 的系統上轉換字元的編碼。

你能夠通過如下的命令用 iconv(1) 來轉換文字檔案的編碼。

$ iconv -f encoding1 -t encoding2 input.txt >output.txt

編碼值是大小寫不敏感的,且會在匹配時忽略“-”和“_”。可以使用“iconv -l”命令檢查支援的編碼。


[注意] 注意

一些編碼只支援資料轉換,它不能作為語言環境的值 (第 8.3.1 节 “編碼的基礎知識”)。

ASCIIISO-8859 這樣適用於單位元組的字符集,字元編碼和字符集幾乎指的是同一件事情。

For character sets with many characters such as JIS X 0213 for Japanese or Universal Character Set (UCS, Unicode, ISO-10646-1) for practically all languages, there are many encoding schemes to fit them into the sequence of the byte data.

對於以上這些,字符集和字元編碼之間有著明顯的區別。

The code page is used as the synonym to the character encoding tables for some vendor specific ones.

[注意] 注意

Please note most encoding systems share the same code with ASCII for the 7 bit characters. But there are some exceptions. If you are converting old Japanese C programs and URLs data from the casually-called shift-JIS encoding format to UTF-8 format, use "CP932" as the encoding name instead of "shift-JIS" to get the expected results: 0x5C → "\" and 0x7E → "~". Otherwise, these are converted to wrong characters.

[提示] 提示

recode(1) 也可能被使用並且不僅僅是 iconv(1)fromdos(1)todos(1)frommac(1)tomac(1) 功能的結合。想要獲得更多資訊,請參見 "info recode"。

vim 這樣的現代智慧編輯器軟體是相當聰明的並且能夠處理任何編碼系統以及任何檔案格式。你應該在支援 UTF-8 編碼的控制檯上並在 UTF-8 環境下使用這些編輯器來獲得最好的相容性。

以 latin1(iso-8859-1)編碼儲存的舊西歐語言的 Unix 文字檔案,“u-file.txt”,能通過如下所示的用 vim 輕易的編輯。

$ vim u-file.txt

這是可能的因為 vim 的檔案編碼自動檢測機制先假定檔案是 UTF-8 編碼,如果失敗了,則假定它是 latin1 編碼。

以 latin2(iso-8859-2) 編碼儲存的舊波蘭語的 Unix 文字檔案,“pu-file.txt”,能通過如下所示的用 vim 編輯。

$ vim '+e ++enc=latin2 pu-file.txt'

An old Japanese unix text file, "ju-file.txt", stored in the eucJP encoding can be edited with vim by the following.

$ vim '+e ++enc=eucJP ju-file.txt'

An old Japanese MS-Windows text file, "jw-file.txt", stored in the so called shift-JIS encoding (more precisely: CP932) can be edited with vim by the following.

$ vim '+e ++enc=CP932 ++ff=dos jw-file.txt'

When a file is opened with "++enc" and "++ff" options, ":w" in the Vim command line stores it in the original format and overwrite the original file. You can also specify the saving format and the file name in the Vim command line, e.g., ":w ++enc=utf8 new.txt".

Please refer to the mbyte.txt "multi-byte text support" in vim on-line help and 表 11.2 “編碼值和用法的列表” for locale values used with "++enc".

The emacs family of programs can perform the equivalent functions.

你可以通過如下所示的來高亮並格式化純文字資料。

表 11.6. 高亮純文字資料的工具列表

軟件包 流行度 大小 關鍵詞 說明
vim-runtime V:20, I:433 27580 高亮 Vim MACRO to convert source code to HTML with ":source $VIMRUNTIME/syntax/html.vim"
cxref V:0, I:0 1174 c→html 從 C 程式到 latex 和 HTML 的轉換器(C語言)
src2tex V:0, I:0 622 高亮 轉換許多原始碼到 TeX(C語言)
source-highlight V:1, I:7 2008 高亮 convert many source codes to HTML, XHTML, LaTeX, Texinfo, ANSI color escape sequences and DocBook files with highlight (C++)
highlight V:1, I:16 997 高亮 轉化許多原始碼到帶有高亮顯示的 HTML, XHTML, RTF, LaTeX, TeX or XSL-FO 檔案。(C++)
grc V:0, I:2 104 text→color generic colouriser for everything (Python)
txt2html V:0, I:4 296 text→html 文字到 HTML 轉換器(Perl)
markdown V:0, I:7 56 text→html markdown text document formatter to (X)HTML (Perl)
asciidoc V:0, I:14 63 text→any AsciiDoc text document formatter to XML/HTML (Python)
pandoc V:4, I:26 69422 text→any general markup converter (Haskell)
python-docutils V:41, I:569 1725 text→any ReStructured Text document formatter to XML (Python)
txt2tags V:0, I:1 951 text→any document conversion from text to HTML, SGML, LaTeX, man page, MoinMoin, Magic Point and PageMaker (Python)
udo V:0, I:0 569 text→any universal document - text processing utility (C language)
stx2any V:0, I:0 264 text→any document converter from structured plain text to other formats (m4)
rest2web V:0, I:0 526 text→html document converter from ReStructured Text to html (Python)
aft V:0, I:0 235 text→any "free form" document preparation system (Perl)
yodl V:0, I:0 547 text→any pre-document language and tools to process it (C language)
sdf V:0, I:0 1445 text→any simple document parser (Perl)
sisu V:0, I:0 5338 text→any document structuring, publishing and search framework (Ruby)

The Extensible Markup Language (XML) is a markup language for documents containing structured information.

See introductory information at XML.COM.

XML text looks somewhat like HTML. It enables us to manage multiple formats of output for a document. One easy XML system is the docbook-xsl package, which is used here.

Each XML file starts with standard XML declaration as the following.

<?xml version="1.0" encoding="UTF-8"?>

The basic syntax for one XML element is marked up as the following.

<name attribute="value">content</name>

XML element with empty content is marked up in the following short form.

<name attribute="value"/>

The "attribute="value"" in the above examples are optional.

The comment section in XML is marked up as the following.

<!-- comment -->

Other than adding markups, XML requires minor conversion to the content using predefined entities for following characters.


[小心] 小心

"<" or "&" can not be used in attributes or elements.

[注意] 注意

When SGML style user defined entities, e.g. "&some-tag:", are used, the first definition wins over others. The entity definition is expressed in "<!ENTITY some-tag "entity value">".

[注意] 注意

As long as the XML markup are done consistently with certain set of the tag name (either some data as content or attribute value), conversion to another XML is trivial task using Extensible Stylesheet Language Transformations (XSLT).

There are many tools available to process XML files such as the Extensible Stylesheet Language (XSL).

Basically, once you create well formed XML file, you can convert it to any format using Extensible Stylesheet Language Transformations (XSLT).

The Extensible Stylesheet Language for Formatting Objects (XSL-FO) is supposed to be solution for formatting. The fop package is new to the Debian main archive due to its dependence to the Java programing language. So the LaTeX code is usually generated from XML using XSLT and the LaTeX system is used to create printable file such as DVI, PostScript, and PDF.


Since XML is subset of Standard Generalized Markup Language (SGML), it can be processed by the extensive tools available for SGML, such as Document Style Semantics and Specification Language (DSSSL).


[提示] 提示

GNOME's yelp is sometimes handy to read DocBook XML files directly since it renders decently on X.

Unix上的 troff 程式最初是由 AT&T 公司開發的,可以被用做簡單排版。現在被用來建立手冊頁。

Donald Knuth 發明的 Tex 是非常強大的排版工具也是實際上的標準。最初是由 Leslie Lamport 開發的 LaTex 使得使用者可以更為方便的利用 Tex 的強大功能。


傳統意義上,roff 是 Unix 上主要的文字處理系統。參見 roff(7), groff(7), groff(1), grotty(1), troff(1), groff_mdoc(7), groff_man(7), groff_ms(7), groff_me(7), groff_mm(7) 和 "info groff"。

You can read or print a good tutorial and reference on "-me" macro in "/usr/share/doc/groff/" by installing the groff package.

[提示] 提示

"groff -Tascii -me -" produces plain text output with ANSI escape code. If you wish to get manpage like output with many "^H" and "_", use "GROFF_NO_SGR=1 groff -Tascii -me -" instead.

[提示] 提示

To remove "^H" and "_" from a text file generated by groff, filter it by "col -b -x".

The TeX Live software distribution offers a complete TeX system. The texlive metapackage provides a decent selection of the TeX Live packages which should suffice for the most common tasks.

這裡有許多可用的 TeXLaTeX 的參考資料。

  • The teTeX HOWTO: The Linux-teTeX Local Guide

  • tex(1)

  • latex(1)

  • texdoc(1)

  • texdoctk(1)

  • "The TeXbook", 作者 Donald E. Knuth, (Addison-Wesley)

  • "LaTeX - A Document Preparation System", 作者 Leslie Lamport, (Addison-Wesley)

  • "The LaTeX Companion", 作者 Goossens, Mittelbach, Samarin, (Addison-Wesley)

This is the most powerful typesetting environment. Many SGML processors use this as their back end text processor. Lyx provided by the lyx package and GNU TeXmacs provided by the texmacs package offer nice WYSIWYG editing environment for LaTeX while many use Emacs and Vim as the choice for the source editor.

有許多線上資源存在。

When documents become bigger, sometimes TeX may cause errors. You must increase pool size in "/etc/texmf/texmf.cnf" (or more appropriately edit "/etc/texmf/texmf.d/95NonPath" and run update-texmf(8)) to fix this.

[注意] 注意

The TeX source of "The TeXbook" is available at http://tug.ctan.org/tex-archive/systems/knuth/dist/tex/texbook.tex. This file contains most of the required macros. I heard that you can process this document with tex(1) after commenting lines 7 to 10 and adding "\input manmac \proofmodefalse". It's strongly recommended to buy this book (and all other books from Donald E. Knuth) instead of using the online version but the source is a great example of TeX input!

Printable data is expressed in the PostScript format on the Debian system. Common Unix Printing System (CUPS) uses Ghostscript as its rasterizer backend program for non-PostScript printers.

你能夠使用 Ghostscript 中的 gs(1) 來合併兩個 PostScript(PS)可移植文件格式(PDF) 檔案。

$ gs -q -dNOPAUSE -dBATCH -sDEVICE=pswrite -sOutputFile=bla.ps -f foo1.ps foo2.ps
$ gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=bla.pdf -f foo1.pdf foo2.pdf
[注意] 注意

PDF 是用途很廣的跨平臺可印刷的資料格式,它本質上是帶有一些額外特性和擴充套件的壓縮了的 PS 格式。

[提示] 提示

對於命令列來說,psmerge(1)psutils 包中的其他命令在處理 PostScript 文件時是很有用的。pdftk 包中的 pdftk(1) 在處理 PDF 文件的時候同樣是很好用的。

如下是處理可印刷資料的工具列表。


Unix 通用列印系統(CUPS) 中的 lp(1)lpr(1) 命令都提供了自定義列印資料的選項。

你可以使用下列命令中的一個來列印 3 份有裝訂頁碼的檔案。

$ lp -n 3 -o Collate=True filename
$ lpr -#3 -o Collate=True filename

你能夠通過 "-o number-up=2", "-o page-set=even", "-o page-set=odd", "-o scaling=200", "-o natural-scaling=200" 等等印表機選項來進一步定製印表機操作,詳細的文件參見命令列列印和選項

下列郵件資料轉換軟體包捕獲了我的眼球。


[提示] 提示

如果郵件客戶端可以配置使用 IMAP4 伺服器,網際網路訊息訪問協議 版本 4 (IMAP4) 伺服器 (參見 第 6.7 节 “POP3/IMAP4 伺服器”) 可以用來把郵件從專有郵件系統裡面移出來。

郵件 (SMTP) 資料需要被限制為 7 位資料序列。二進位制資料和 8 位文字資料使用 Multipurpose Internet Mail Extensions (MIME) 網際網路多用途郵件擴充套件 和選擇的字符集編碼到 7 位格式。(參見 第 8.3.1 节 “編碼的基礎知識”).

標準的郵件儲存格式是 mbox,它是依據 RFC2822 (由 RFC822 更新) 來的格式. 參見 mbox(5) (由 mutt 軟體包提供).

對於歐洲語言,由於沒有什麼 8 位字元,"Content-Transfer-Encoding: quoted-printable" 加 ISO-8859-1 字符集通常被用於郵件。如果歐洲文字是被編碼為 UTF-8,由於幾乎全是 7 位資料,使用 "Content-Transfer-Encoding: quoted-printable" 也是合適的。

對於日語,傳統的 "Content-Type: text/plain; charset=ISO-2022-JP" 通常被用於郵件來保持文字在 7 位。但是老的微軟系統會在沒有宣告的情況下使用 Shift-JIS 來發送郵件。如果日語文字是用 UTF-8 編碼, 由於含有許多 8 位資料,使用 Base64 是合適的。其它亞洲語言也是類似情形。

[注意] 注意

如果你的非 Unix 郵件資料可以通過一個具備和 IMAP4 服務通訊的非 Debian 客戶端訪問,你可以通過執行你的 IMAP4 服務來將郵件資料移出。(參見 第 6.7 节 “POP3/IMAP4 伺服器”).

[注意] 注意

如果你使用其它郵件儲存格式,第一步把它們移動到 mbox 格式比較好。像 mutt(1) 這樣多功能的客戶端程式可以便捷的完成這類操作。

你可以使用 procmail(1)formail(1) 把郵箱內容分開成每一封郵件.

每一封郵件能夠使用來自 mpack 軟體包的 munpack(1) 命令(或其它特異的工具)來獲得 MIME 編碼內容。

如下是關於圖形資料轉換、編輯和管理的工具包。

表 11.17. 圖形資料工具列表

軟件包 流行度 大小 關鍵詞 說明
gimp V:91, I:507 16278 圖形(點陣圖) GNU 圖形處理程式
imagemagick V:142, I:538 192 圖形(點陣圖) 圖形處理程式
graphicsmagick V:7, I:15 4821 圖形(點陣圖) 影象處理程式(imagemagick派生出來的)
xsane V:22, I:191 909 圖形(點陣圖) GTK+-based X11 frontend for SANE (Scanner Access Now Easy)
netpbm V:29, I:541 4302 圖形(點陣圖) 圖形介面的轉換工具
icoutils V:8, I:80 191 png↔ico(bitmap) convert MS Windows icons and cursors to and from PNG formats (favicon.ico)
scribus V:13, I:29 19136 ps/pdf/SVG/… Scribus DTP 編輯器
libreoffice-draw V:320, I:478 8970 圖形(向量) LibreOffice 辦公套件-繪畫
inkscape V:141, I:354 129840 圖形(向量) SVG(可升級向量圖形)編輯器
dia-gnome V:5, I:10 20 圖形(向量) 圖表編輯器(GNOME)
dia V:25, I:41 3881 圖形(向量) 圖表編輯器(Gtk)
xfig V:12, I:19 1795 圖形(向量) Facility for Interactive Generation of figures under X11
pstoedit V:13, I:339 667 ps/pdf→image(向量) PostScript 和 PDF 檔案到可編輯的向量圖形的轉換器(SVG)
libwmf-bin V:13, I:359 104 Windows/image(vector) Windows metafile (vector graphic data) conversion tools
fig2sxd V:0, I:0 149 fig→sxd(vector) 轉換 XFig 檔案為 OpenOffice.org 繪畫格式
unpaper V:2, I:16 453 image→image post-processing tool for scanned pages for OCR
tesseract-ocr V:4, I:28 558 image→text 基於惠普的商業 OCR 引擎的免費 OCR 軟體
tesseract-ocr-eng I:29 37486 image→text OCR engine data: tesseract-ocr language files for English text
gocr V:1, I:23 477 image→text 免費 OCR 軟體
ocrad V:1, I:7 299 image→text 免費 OCR 軟體
eog V:95, I:329 10582 image(Exif) Eye of GNOME 影象瀏覽程式
gthumb V:15, I:26 3269 image(Exif) 影象瀏覽器(GNOME)
geeqie V:16, I:24 1588 image(Exif) 基於 GTK+ 的影象瀏覽器
shotwell V:15, I:126 5762 image(Exif) 數碼相片管理器(GNOME)
gtkam V:0, I:7 1079 image(Exif) application for retrieving media from digital cameras (GTK+)
gphoto2 V:1, I:14 969 image(Exif) gphoto2 軟體是命令列方式的管理數碼相機的工具
gwenview V:29, I:104 4509 image(Exif) 圖片瀏覽器(KDE)
kamera V:3, I:104 230 image(Exif) KDE 上的支援數碼相機的應用軟體
digikam V:4, I:17 1760 image(Exif) 用於 KDE 桌面環境的數字照片管理應用
exiv2 V:5, I:71 247 image(Exif) EXIF/IPTC 元資料處理工具
exiftran V:2, I:25 71 image(Exif) transform digital camera jpeg images
jhead V:1, I:13 105 image(Exif) manipulate the non-image part of Exif compliant JPEG (digital camera photo) files
exif V:1, I:10 236 image(Exif) 顯示 JPEG 檔案中的 EXIF 資訊的命令列工具
exiftags V:0, I:4 288 image(Exif) utility to read Exif tags from a digital camera JPEG file
exifprobe V:0, I:3 482 image(Exif) 從數碼圖片中讀取元資料
dcraw V:3, I:24 384 image(Raw)→ppm decode raw digital camera images
findimagedupes V:0, I:1 79 image→fingerprint 找到相似或重複的影象
ale V:0, I:0 753 image→image merge images to increase fidelity or create mosaics
imageindex V:0, I:0 144 image(Exif)→html generate static HTML galleries from images
outguess V:0, I:1 216 jpeg,png universal Steganographic tool
librecad V:11, I:18 8039 DXF CAD 資料編輯器(KDE)
blender V:4, I:31 101399 blend, TIFF, VRML, … 3D content editor for animation etc
mm3d V:0, I:0 5314 ms3d, obj, dxf, … 基於 OpenGL 的 3D 模型編輯器
open-font-design-toolkit I:0 11 ttf, ps, … metapackage for open font design
fontforge V:1, I:10 91 ttf, ps, … 用於 PS,TrueType 和 OpenType 的字型編輯器
xgridfit V:0, I:0 898 ttf program for gridfitting and hinting TrueType fonts

[提示] 提示

aptitude(8) (參考第 2.2.6 节 “aptitude 搜尋方式選項”)中用正則表示式 "~Gworks-with::image" 來查詢更多的影象工具。

雖然像 gimp(1) 這樣的圖形介面程式是非常強大的,但像 imagemagick(1) 這樣的命令列工具在用指令碼自動化處理影象時是很有用的。

實際上的數碼相機的影象是可交換的影象檔案格式(EXIF),這種格式是在 JPEG 影象檔案格式上新增一些元資料標籤。它能夠儲存諸如日期、時間和相機設定的資訊。

The Lempel-Ziv-Welch (LZW)無損資料壓縮專利已經過期了。使用 LZW 壓縮方式的 圖形互動格式(GIF)工具現在可以在 Debian 系統上自由使用了。

[提示] 提示

任何帶有可移動記錄介質的數碼相機或掃描器都可以在 Linux 上通過 USB 儲存讀取器來工作,因為它遵循相機檔案系統設計規則並且使用 FAT 檔案系統,參考第 10.1.7 节 “可移動儲存裝置”

這裡有許多其他用於資料轉換的工具。在 aptitude(8)(參考 第 2.2.6 节 “aptitude 搜尋方式選項”) 裡用正則表示式 "~Guse::converting"" 來查詢如下的軟體包。


你能夠通過如下的命令從 RPM 格式的包中提取資料。

$ rpm2cpio file.src.rpm | cpio --extract