fold

The fold command, which is found on Mac OS X and Linux systems, allows you to "fold" the contents of specified files, or the standard input if no files are specified, breaking the lines to have a maximum of eighty characters/columns by default, though you can also specify that the line breaks be made at some other character width with the -w width option.

E.g. assuming I have the following text, which is from Ion by Plato, with the text just wrapping from line to line with no line breaks specified.

Many are the noble words in which poets speak concerning the actions of men; but like yourself when speaking about Homer, they do not speak of them by any rules of art: they are simply inspired to utter that to which the Muse impels them, and that only; and when inspired, one of them will make dithyrambs, another hymns of praise, another choral strains, another epic or iambic verses- and he who is good at one is not good any other kind of verse: for not by art does the poet sing, but by power divine. Had he learned by rules of art, he would have known how to speak not of one theme only, but of all; and therefore God takes away the minds of poets, and uses them as his ministers, as he also uses diviners and holy prophets, in order that we who hear them may know them to be speaking not of themselves who utter these priceless words in a state of unconsciousness, but that God himself is the speaker, and that through them he is conversing with us. And Tynnichus the Chalcidian affords a striking instance of what I am saying: he wrote nothing that any one would care to remember but the famous paean which; in every one's mouth, one of the finest poems ever written, simply an invention of the Muses, as he himself says. For in this way, the God would seem to indicate to us and not allow us to doubt that these beautiful poems are not human, or the work of man, but divine and the work of God; and that the poets are only the interpreters of the Gods by whom they are severally possessed. Was not this the lesson which the God intended to teach when by the mouth of the worst of poets he sang the best of songs? Am I not right, Ion?

If the text is stored in a file named socrates-ion.txt, I can create another file with line breaks at column 80 with the command fold socrates-ion.txt >socrates-ion_80.txt. The paragraph above would then be formatted as follows:

Many are the noble words in which poets speak concerning the actions of men; but
 like yourself when speaking about Homer, they do not speak of them by any rules
 of art: they are simply inspired to utter that to which the Muse impels them, a
nd that only; and when inspired, one of them will make dithyrambs, another hymns
 of praise, another choral strains, another epic or iambic verses- and he who is
 good at one is not good any other kind of verse: for not by art does the poet s
ing, but by power divine. Had he learned by rules of art, he would have known ho
w to speak not of one theme only, but of all; and therefore God takes away the m
inds of poets, and uses them as his ministers, as he also uses diviners and holy
 prophets, in order that we who hear them may know them to be speaking not of th
emselves who utter these priceless words in a state of unconsciousness, but that
 God himself is the speaker, and that through them he is conversing with us. And
 Tynnichus the Chalcidian affords a striking instance of what I am saying: he wr
ote nothing that any one would care to remember but the famous paean which; in e
very one's mouth, one of the finest poems ever written, simply an invention of t
he Muses, as he himself says. For in this way, the God would seem to indicate to
 us and not allow us to doubt that these beautiful poems are not human, or the w
ork of man, but divine and the work of God; and that the poets are only the inte
rpreters of the Gods by whom they are severally possessed. Was not this the less
on which the God intended to teach when by the mouth of the worst of poets he sa
ng the best of songs? Am I not right, Ion?

Each line ends at column 80, but having words split over lines is not what I want. Instead, I would like to have the line breaks occur at spaces between words. The fold utility allows you to achieve that end using the -s option.

     -s      Fold line after the last blank character within the first width
             column positions (or bytes).

So, if I use fold -s socrates-ion.txt >socrates-ion_80s.txt, the paragraph in the output file would look as follows:

Generic Category (English)120x600
Many are the noble words in which poets speak concerning the actions of men;
but like yourself when speaking about Homer, they do not speak of them by any
rules of art: they are simply inspired to utter that to which the Muse impels
them, and that only; and when inspired, one of them will make dithyrambs,
another hymns of praise, another choral strains, another epic or iambic verses-
and he who is good at one is not good any other kind of verse: for not by art
does the poet sing, but by power divine. Had he learned by rules of art, he
would have known how to speak not of one theme only, but of all; and therefore
God takes away the minds of poets, and uses them as his ministers, as he also
uses diviners and holy prophets, in order that we who hear them may know them
to be speaking not of themselves who utter these priceless words in a state of
unconsciousness, but that God himself is the speaker, and that through them he
is conversing with us. And Tynnichus the Chalcidian affords a striking instance
of what I am saying: he wrote nothing that any one would care to remember but
the famous paean which; in every one's mouth, one of the finest poems ever
written, simply an invention of the Muses, as he himself says. For in this way,
the God would seem to indicate to us and not allow us to doubt that these
beautiful poems are not human, or the work of man, but divine and the work of
God; and that the poets are only the interpreters of the Gods by whom they are
severally possessed. Was not this the lesson which the God intended to teach
when by the mouth of the worst of poets he sang the best of songs? Am I not
right, Ion?

On a Linux system, you can get help on the command by typing fold --help, though the --help option isn't supported on OS X.

$ fold --help
Usage: fold [OPTION]... [FILE]...
Wrap input lines in each FILE (standard input by default), writing to
standard output.

Mandatory arguments to long options are mandatory for short options too.
  -b, --bytes         count bytes rather than columns
  -c, --characters    count characters rather than columns
  -s, --spaces        break at spaces
  -w, --width=WIDTH   use WIDTH columns instead of 80
      --help     display this help and exit
      --version  output version information and exit

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'fold invocation'

For CentOS Linux, the utility is provided by the coreutils package.

# rpm -q --whatprovides /bin/fold
coreutils-8.22-11.el7.x86_64
#

 

TechRabbit ad 300x250 newegg.com

Justdeals Daily Electronics Deals1x1 px