6

what is the command that word wraps and justifies a text file so that the output looks like that of a man page:

   All of these system  calls  are  used  to  wait  for  state
   changes  in  a  child  of  the  calling process, and obtain
   information about the child whose  state  has  changed.   A
   state change is considered to be: the child terminated; the
   child was stopped by a signal; or the child was resumed  by
   a  signal.  In the case of a terminated child, performing a
   wait allows the system to release the resources  associated
   with the child; if a wait is not performed, then the termi-
   nated child remains in a "zombie" state (see NOTES below).

4 Answers 4

5

Use the fold command to specify a maximum allowed width:

perl -e 'print "a"x5, " " for 1..99' | fold -w 30

And you got: (Instead of a single line)

aaaaa aaaaa aaaaa aaaaa aaaaa 
aaaaa aaaaa aaaaa aaaaa aaaaa 
aaaaa aaaaa aaaaa aaaaa aaaaa 
aaaaa aaaaa aaaaa aaaaa aaaaa 
aaaaa aaaaa aaaaa aaaaa aaaaa 
aaaaa aaaaa aaaaa aaaaa aaaaa 
aaaaa aaaaa aaaaa aaaaa aaaaa 
aaaaa aaaaa aaaaa aaaaa aaaaa 
aaaaa aaaaa aaaaa aaaaa aaaaa 
aaaaa aaaaa aaaaa aaaaa aaaaa 
aaaaa aaaaa aaaaa aaaaa aaaaa 
aaaaa aaaaa aaaaa aaaaa aaaaa 
aaaaa aaaaa aaaaa aaaaa aaaaa 
aaaaa aaaaa aaaaa aaaaa aaaaa 
aaaaa aaaaa aaaaa aaaaa aaaaa 
aaaaa aaaaa aaaaa aaaaa aaaaa 
aaaaa aaaaa aaaaa aaaaa aaaaa 
aaaaa aaaaa aaaaa aaaaa aaaaa 
aaaaa aaaaa aaaaa aaaaa aaaaa 
aaaaa aaaaa aaaaa aaaaa 
1
  • 3
    Worth to mention, you have to tell fold to break at spaces (with -s), otherwise it will not respect words but simply break a line at the given width. So, for example, try fold -s -w 72 test_docu.txt | cat (or replace cat with less, or whatever you prefer). As for the man page part of your (@cody) question: you can locate a man page with whereis, and then just open it in an editor (it is not necessary to gunzip, if yours are .gz just like mine). Then you'll see the groff syntax, and it is possible to get a grip on pretty quickly. You'll get stuff like hyphenation, tables, etc. Oct 14, 2012 at 0:45
4

That would be GROFF.
(Older Unix version use NROFF or TROFF, but they are all different programs in the same family.)

Please note that it is not a simple command that operates on a text-file. It requires formatting instructions to be inserted in the text.
In fact it operates much more like a compiler.
If you are familiar with TeX or LaTeX than you know the principle, but the various ROFF style programms have a much simpler command language.

2
  • Can you add a simple example? If I run groff -T utf8 -K utf8 somefile I get a bunch of empty lines that are not present in the input file. I guess groff somehow creates a “letter page-sized” file.
    – Marco
    Oct 14, 2012 at 9:01
  • @Marco: Check out my comment to warl0cks answer: easiest way to get started with groff is to copy a man page, edit it, and then view it as you usually do (man in the shell, or M-x man in Emacs). The groff syntax is not always intuitive (less so, I would say, than HTML or LaTeX) but if you see both the page and the source live, you'll be able to figure out a lot without documentation. Oct 14, 2012 at 18:33
3

If all you need is justify text without having to write it in roff, you could use par (not a standard command but it's packaged in most Linux distributions).

$ seq 120 | par -j -w40
1 2 3 4  5 6 7 8 9 10 11 12  13 14 15 16
17 18 19  20 21 22 23 24 25  26 27 28 29
30 31 32  33 34 35 36 37 38  39 40 41 42
43 44 45  46 47 48 49 50 51  52 53 54 55
56 57 58  59 60 61 62 63 64  65 66 67 68
69 70 71  72 73 74 75 76 77  78 79 80 81
82 83 84  85 86 87 88 89 90  91 92 93 94
95 96 97  98 99 100 101 102  103 104 105
106 107 108 109 110  111 112 113 114 115
116 117 118 119 120

Note that neither fold nor fmt justify. par has a lot of options to format comments, emails, lists...

2
  • 3
    1) par does only handle ASCII, not Unicode. One has to recompile it with a patch to get Unicode support. 2) par does not hyphenate.
    – Marco
    Oct 14, 2012 at 8:57
  • 1
    nicemice.net/par/par-doc.var: "The documentation, though precise, is unfortunately not well-written for the end-user. Your best bet is probably to read quickly the Description, Terminology, Options, and Environment sections, then read carefully the Examples section, referring back to the Options and Terminology sections as needed."
    – denis
    Nov 21, 2016 at 14:21
0

FMT(1) User Commands FMT(1)

NAME fmt - simple optimal text formatter

1
  • 2
    You may want to expand on that a little.
    – Ansgar Wiechers
    Oct 13, 2012 at 22:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .