pr - ファイルを印刷用に整形する

スポンサーリンク

prコマンドは、ファイルの内容を印刷用に適した形式に整形を行います。

実際に印刷をするコマンドではありません。

prコマンドとは

prコマンドは、引数にあるファイルや標準入力を印刷用に適した形式としてページ付けやマルチカラム形式に整形して、標準出力に書き出します。ただし、実際の印刷を行う場合に利用できるかどうかはまた別問題かもしれません。

印刷用に整形
(オプションなし)

prコマンドは、デフォルトで利用するとファイルの内容をそれぞれのページに分けて印刷用に整形します。
それぞれのページには5行のヘッダーをつけます。ヘッダーは2行の空行の後に、1行で日付、ファイル名、ページ番号を書き出し、さらに2行の空行を入れます。また、5行の空行であるフッターも入れます。
コマンド例で利用しているファイルはplatoの「The Republish」の導入の最初の方を使用しています。

コマンド例

the_republic.txt

THE INTRODUCTION

The Republic of Plato is the longest of his works with the exception
of the Laws, and is certainly the greatest of them. There are nearer
approaches to modern metaphysics in the Philebus and in the Sophist;
the Politicus or Statesman is more ideal; the form and institutions
of the State are more clearly drawn out in the Laws; as works of art,
the Symposium and the Protagoras are of higher excellence. But no
other Dialogue of Plato has the same largeness of view and the same
perfection of style; no other shows an equal knowledge of the world,
or contains more of those thoughts which are new as well as old, and
not of one age only but of all. Nowhere in Plato is there a deeper
irony or a greater wealth of humor or imagery, or more dramatic power.
Nor in any other of his writings is the attempt made to interweave
life and speculation, or to connect politics with philosophy. The
Republic is the centre around which the other Dialogues may be grouped;
here philosophy reaches the highest point to which ancient thinkers
ever attained. Plato among the Greeks, like Bacon among the moderns,
was the first who conceived a method of knowledge, although neither
of them always distinguished the bare outline or form from the substance
of truth; and both of them had to be content with an abstraction of
science which was not yet realized. He was the greatest metaphysical
genius whom the world has seen; and in him, more than in any other
ancient thinker, the germs of future knowledge are contained. The
sciences of logic and psychology, which have supplied so many instruments
of thought to after-ages, are based upon the analyses of Socrates
and Plato. The principles of definition, the law of contradiction,
the fallacy of arguing in a circle, the distinction between the essence
and accidents of a thing or notion, between means and ends, between
causes and conditions; also the division of the mind into the rational,
concupiscent, and irascible elements, or of pleasures and desires
into necessary and unnecessary --these and other great forms of thought
are all of them to be found in the Republic, and were probably first
invented by Plato. The greatest of all logical truths, and the one
of which writers on philosophy are most apt to lose sight, the difference
between words and things, has been most strenuously insisted on by
him, although he has not always avoided the confusion of them in his
own writings. But he does not bind up truth in logical formulae, --logic
is still veiled in metaphysics; and the science which he imagines
to "contemplate all truth and all existence" is very unlike the doctrine
of the syllogism which Aristotle claims to have discovered.

Neither must we forget that the Republic is but the third part of
a still larger design which was to have included an ideal history
of Athens, as well as a political and physical philosophy. The fragment
of the Critias has given birth to a world-famous fiction, second only
in importance to the tale of Troy and the legend of Arthur; and is
said as a fact to have inspired some of the early navigators of the
sixteenth century. This mythical tale, of which the subject was a
history of the wars of the Athenians against the Island of Atlantis,
is supposed to be founded upon an unfinished poem of Solon, to which
it would have stood in the same relation as the writings of the logographers
to the poems of Homer. It would have told of a struggle for Liberty,
intended to represent the conflict of Persia and Hellas. We may judge
from the noble commencement of the Timaeus, from the fragment of the
Critias itself, and from the third book of the Laws, in what manner
Plato would have treated this high argument. We can only guess why
the great design was abandoned; perhaps because Plato became sensible
of some incongruity in a fictitious history, or because he had lost
his interest in it, or because advancing years forbade the completion
of it; and we may please ourselves with the fancy that had this imaginary
narrative ever been finished, we should have found Plato himself sympathizing
with the struggle for Hellenic independence, singing a hymn of triumph
over Marathon and Salamis, perhaps making the reflection of Herodotus
where he contemplates the growth of the Athenian empire--"How brave
a thing is freedom of speech, which has made the Athenians so far
exceed every other state of Hellas in greatness!" or, more probably,
attributing the victory to the ancient good order of Athens and to
the favor of Apollo and Athene.

Again, Plato may be regarded as the "captain" ('arhchegoz') or leader
of a goodly band of followers; for in the Republic is to be found
the original of Cicero's De Republica, of St. Augustine's City of
God, of the Utopia of Sir Thomas More, and of the numerous other imaginary
States which are framed upon the same model. The extent to which Aristotle
or the Aristotelian school were indebted to him in the Politics has
been little recognized, and the recognition is the more necessary
because it is not made by Aristotle himself. The two philosophers
had more in common than they were conscious of; and probably some
elements of Plato remain still undetected in Aristotle. In English
philosophy too, many affinities may be traced, not only in the works
of the Cambridge Platonists, but in great original writers like Berkeley
or Coleridge, to Plato and his ideas. That there is a truth higher
than experience, of which the mind bears witness to herself, is a
conviction which in our own generation has been enthusiastically asserted,
and is perhaps gaining ground. Of the Greek authors who at the Renaissance
brought a new life into the world Plato has had the greatest influence.
The Republic of Plato is also the first treatise upon education, of
which the writings of Milton and Locke, Rousseau, Jean Paul, and Goethe
are the legitimate descendants. Like Dante or Bunyan, he has a revelation
of another life; like Bacon, he is profoundly impressed with the un
unity of knowledge; in the early Church he exercised a real influence
on theology, and at the Revival of Literature on politics. Even the
fragments of his words when "repeated at second-hand" have in all
ages ravished the hearts of men, who have seen reflected in them their
own higher nature. He is the father of idealism in philosophy, in
politics, in literature. And many of the latest conceptions of modern
thinkers and statesmen, such as the unity of knowledge, the reign
of law, and the equality of the sexes, have been anticipated in a
dream by him.

実行結果

2列以上で表示
(--columnsオプション)

--columnsオプション(または-2,-3のような数字のオプション)はファイルの表示を2列以上にすることができます。1列にある行が長すぎる場合は、切り捨てられるので注意が必要です。切り捨てられたくない場合はfmtコマンドを用いるとよいかもしれません。

コマンド例

または

実行結果

whiptailコマンドと組み合わせてみる

whiptailコマンドはシェルで、ダイアログ画面を表示させるコマンドになります。whiptailコマンドでマルチカラムのテキストを表示させたい場合にprコマンドを利用できるかもしれません。

ここでは、テキストファイルを2列の形式で表示させたいと思います。また、whiptailコマンドで表示させるためにfmtコマンドで行を切り捨てられないように前処理し、さらにexpandコマンドでタブをスペースに変換する後処理を行っています。

コマンド例

実行結果

whiptail-pr

端末の高さを50文字、幅を80文字に設定しています。また、実行した後はTABでOKボタンに移動し、終了できます。

prコマンドの-lオプションはページの長さを設定するオプションです。

参考

GNU Coreutils: pr invocation