Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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).
share|improve this question
add comment

migrated from serverfault.com Oct 14 '12 at 0:06

This question came from our site for professional system and network administrators.

4 Answers

up vote 3 down vote accepted

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.

share|improve this answer
    
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 '12 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. –  Emanuel Berg Oct 14 '12 at 18:33
add comment

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 
share|improve this answer
    
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. –  Emanuel Berg Oct 14 '12 at 0:45
add comment

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...

share|improve this answer
2  
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 '12 at 8:57
add comment

FMT(1) User Commands FMT(1)

NAME fmt - simple optimal text formatter

share|improve this answer
1  
You may want to expand on that a little. –  Ansgar Wiechers Oct 13 '12 at 22:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.