4

我想做的是以下。

文本文件内容:

This is a simple text file
containing lines of text
with different width
but I would like to justify
them. Any idea ?

预期结果 :

This is a simple text file containing
lines of  text with  different  width
but I  would  like  to  justify them.
Any Idea ?

我已经可以使用以下方法将文件拆分为所需的宽度:

cat textfile|fmt -s -w 37

但在那种情况下,没有任何理由......

编辑:按照建议使用 par ,我发现重音字符有问题。这就是为我提供标准杆 37j1 的原因:

This   is   à  simplé   text   file
containing   lines   of  tèxt   with
different wïdth but I woùld like to
justîfy them. Any idéà ?

不再有道理了...但是无论如何空间都被改变了...

谢谢你的帮助,

诽谤

4

2 回答 2

4

你可以nroff使用它man

(echo '.ll 37'
 echo '.pl 0'
 cat orig.txt) | nroff

根据您的输入产生:

This is a simple text file containing
lines of text  with  different  width
but I would like to justify them. Any
idea ?

以上仅适用于 ASCII。

编辑

如果你想用 nroff 处理 utf8 文本,你可以尝试下一个:

cat orig.txt | (        #yes, i know - UUOC
    echo '.ll 37'     #line length
    echo '.pl 0'      #page length (0-disables empty lines)
    echo '.nh'        #no hypenation
    preconv -e utf8 -
) | groff -Tutf8

从这个 utf8 编码的输入:

Voix ambiguë d'un cœur qui au zéphyr préfère les jattes de kiwi.
Voyez le brick géant que j'examine près du wharf.
Monsieur Jack, vous dactylographiez bien mieux que votre ami Wolf.
Eble ĉiu kvazaŭ-deca fuŝĥoraĵo ĝojigos homtipon..
Laŭ Ludoviko Zamenhof bongustas freŝa ĉeĥa manĝaĵo kun spicoj.
Nechť již hříšné saxofony ďáblů rozezvučí síň úděsnými tóny waltzu, tanga a
quickstepu.

产生:

Voix  ambiguë d’un cœur qui au zéphyr
préfère les jattes de kiwi.  Voyez le
brick  géant  que  j’examine  près du
wharf.     Monsieur    Jack,     vous
dactylographiez  bien mieux que votre
ami  Wolf.   Eble   ĉiu   kvazaŭ‐deca
fuŝĥoraĵo   ĝojigos  homtipon..   Laŭ
Ludoviko  Zamenhof  bongustas   freŝa
ĉeĥa  manĝaĵo  kun spicoj.  Nechť již
hříšné saxofony ďáblů  rozezvučí  síň
úděsnými   tóny   waltzu,   tanga   a
quickstepu.

如果删除该行

echo '.nh'   #no hypenation

你会得到hypenated文本

Voix  ambiguë d’un cœur qui au zéphyr
préfère les jattes de kiwi.  Voyez le
brick  géant  que  j’examine  près du
wharf.  Monsieur Jack, vous  dactylo‐
graphiez  bien  mieux  que  votre ami
Wolf.  Eble ĉiu kvazaŭ‐deca fuŝĥoraĵo
ĝojigos  homtipon..  Laŭ Ludoviko Za‐
menhof bongustas freŝa  ĉeĥa  manĝaĵo
kun  spicoj.   Nechť již hříšné saxo‐
fony  ďáblů  rozezvučí  síň  úděsnými
tóny waltzu, tanga a quickstepu.
于 2014-03-22T18:44:18.440 回答
1

你可以使用par

par -j -w37 < inputfile
  • -j选项将证明段落的合理性。
  • -w表示最大输出行长度。

对于您的输入,它会产生:

This is a simple text file containing
lines  of text  with different  width
but I would like to justify them. Any
idea ?

另一种方法是使用emacs

emacs -batch inputfile --eval '(set-fill-column 37)' --eval '(fill-region (point-min) (point-max))' -f save-buffer

这也会产生:

This is a simple text file containing
lines of text with different width
but I would like to justify them. Any
idea ?
于 2014-03-22T17:53:36.573 回答