sebastiano.tronto.net

Source files and build scripts for my personal website
git clone https://git.tronto.net/sebastiano.tronto.net
Download | Log | Files | Refs | README

fold.md (3564B)


      1 # UNIX text filters, part 2.6 of 3: fold
      2 
      3 *This post is part of a [series](../../series)*
      4 
      5 Today's text filter is `fold`, a program that can be used to format
      6 paragraphs of text so that lines do not exceed a given width.
      7 
      8 ## fold
      9 
     10 First of all, let's take a moment to celebrate the fact that the
     11 [OpenBSD manual page for `fold`](http://man.openbsd.org/OpenBSD-7.4/fold)
     12 uses the same terminology as me:
     13 
     14 ```
     15 DESCRIPTION
     16      fold is a filter [...]
     17 ```
     18 
     19 See? I didn't make this up!
     20 
     21 Anyway, back to the tool. What `fold` does is breaking up lines of text
     22 so that they take up a maximum of 80 characters - or any number of
     23 characters specified by the `-w` option. For example:
     24 
     25 ```
     26 $ echo 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec pretium odio quis nisi vestibulum, at semper magna ornare. Nulla facilisi. Sed in magna lacus. Proin faucibus est non ligula vehicula, quis ultrices lectus ultricies.  Aenean sit amet dignissim mauris. Sed luctus lobortis augue nec aliquet. Cras in felis tellus. Curabitur id purus feugiat enim posuere ultrices in viverra erat. Nulla facilisi. Donec et neque hendrerit, dignissim ipsum id, venenatis enim.' \
     27 | fold -w 72
     28 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec pretium o
     29 dio quis nisi vestibulum, at semper magna ornare. Nulla facilisi. Sed in
     30  magna lacus. Proin faucibus est non ligula vehicula, quis ultrices lect
     31 us ultricies.  Aenean sit amet dignissim mauris. Sed luctus lobortis aug
     32 ue nec aliquet. Cras in felis tellus. Curabitur id purus feugiat enim po
     33 suere ultrices in viverra erat. Nulla facilisi. Donec et neque hendrerit
     34 , dignissim ipsum id, venenatis enim.
     35 ```
     36 
     37 Although you probably want to use the `-s` option so that words are not
     38 broken halfway through:
     39 
     40 ```
     41 $ (same echo command) \
     42 | fold -s -w 72
     43 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec pretium
     44 odio quis nisi vestibulum, at semper magna ornare. Nulla facilisi. Sed
     45 in magna lacus. Proin faucibus est non ligula vehicula, quis ultrices
     46 lectus ultricies.  Aenean sit amet dignissim mauris. Sed luctus
     47 lobortis augue nec aliquet. Cras in felis tellus. Curabitur id purus
     48 feugiat enim posuere ultrices in viverra erat. Nulla facilisi. Donec et
     49 neque hendrerit, dignissim ipsum id, venenatis enim.
     50 ```
     51 
     52 As with
     53 [`rev`](../2024-03-27-rev) and [`cut`](../2024-03-28-cut), the environment
     54 variable `LC_CTYPE` is used to determine what a character is, but
     55 one can also specify to use *bytes* instead using the `-b` option.
     56 
     57 ## Examples
     58 
     59 ### Text formatting
     60 
     61 A classic use for `fold` is formatting emails (or markdown files
     62 for blog posts) to avoid long lines. For example if you use
     63 [vi](https://en.wikipedia.org/wiki/Vi_(text_editor)), the command
     64 
     65 ```
     66 !} fold -s -w 72
     67 ```
     68 
     69 will format the next paragraph. However,
     70 [`fmt`](http://man.openbsd.org/OpenBSD-7.4/fmt) can do the same and it is
     71 more convenient to use - check it out!
     72 
     73 ### Generating passwords
     74 
     75 Here is a cool example that puts together `fold` with some of the
     76 other tools we have seen, [`tr`](../2024-01-13-tr) and
     77 [`head`](../2024-02-20-head-and-tail). To generate a random password,
     78 I use:
     79 
     80 ```
     81 $ cat /dev/random | tr -cd 'a-z0-9' | fold -w 12 | head -1
     82 ```
     83 
     84 This command reads the special file `/dev/random`, which contains a
     85 never-ending stream of random bytes, and passes it through various commands
     86 in a *pipeline*. First, every character that is *not* (`tr -c`) a
     87 lowercase letter or a number (`a-z0-9`) is deleted (`-d`); then
     88 the result is `fold`'d to 12 characters (`fold -w 12`); finally,
     89 the first line is taken (`head -1`).