aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-05-31 21:21:52 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2024-05-31 21:21:52 +0200
commitc6cd23bb8bcfb50da6075765a905d1c07425ba78 (patch)
treea13862bfa8f92ad5d165af683e75b311469779a3
parent0aeeeed253053d31eed23962299d86200820eaa9 (diff)
downloadsebastiano.tronto.net-c6cd23bb8bcfb50da6075765a905d1c07425ba78.tar.gz
sebastiano.tronto.net-c6cd23bb8bcfb50da6075765a905d1c07425ba78.zip
New blog post
-rw-r--r--src/blog/2024-04-07-expand-unexpand/expand-unexpand.md2
-rw-r--r--src/blog/2024-05-31-fold/fold.md89
-rw-r--r--src/series/series.md1
3 files changed, 92 insertions, 0 deletions
diff --git a/src/blog/2024-04-07-expand-unexpand/expand-unexpand.md b/src/blog/2024-04-07-expand-unexpand/expand-unexpand.md
index 19d6f3b..563f8eb 100644
--- a/src/blog/2024-04-07-expand-unexpand/expand-unexpand.md
+++ b/src/blog/2024-04-07-expand-unexpand/expand-unexpand.md
@@ -56,3 +56,5 @@ $ for f in *.rs; do unexpand $f > $f.2 && mv $f.2 $f; done
56For some reason, the OpenBSD version of `unexpand` does not allow 56For some reason, the OpenBSD version of `unexpand` does not allow
57using the `-t` option. So if I had brought my OpenBSD laptop at the 57using the `-t` option. So if I had brought my OpenBSD laptop at the
58rust lecture I would have been stuck with spaces :( 58rust lecture I would have been stuck with spaces :(
59
60*Next in the series: [fold](../2024-05-31-fold)*
diff --git a/src/blog/2024-05-31-fold/fold.md b/src/blog/2024-05-31-fold/fold.md
new file mode 100644
index 0000000..553975b
--- /dev/null
+++ b/src/blog/2024-05-31-fold/fold.md
@@ -0,0 +1,89 @@
1# UNIX text filters, part 2.6 of 3: fold
2
3*This post is part of a [series](../../series)*
4
5Today's text filter is `fold`, a program that can be used to format
6paragraphs of text so that lines do not exceed a given width.
7
8## fold
9
10First 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)
12uses the same terminology as me:
13
14```
15DESCRIPTION
16 fold is a filter [...]
17```
18
19See? I didn't make this up!
20
21Anyway, back to the tool. What `fold` does is breaking up lines of text
22so that they take up a maximum of 80 characters - or any number of
23characters 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
28Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec pretium o
29dio quis nisi vestibulum, at semper magna ornare. Nulla facilisi. Sed in
30 magna lacus. Proin faucibus est non ligula vehicula, quis ultrices lect
31us ultricies. Aenean sit amet dignissim mauris. Sed luctus lobortis aug
32ue nec aliquet. Cras in felis tellus. Curabitur id purus feugiat enim po
33suere ultrices in viverra erat. Nulla facilisi. Donec et neque hendrerit
34, dignissim ipsum id, venenatis enim.
35```
36
37Although you probably want to use the `-s` option so that words are not
38broken halfway through:
39
40```
41$ (same echo command) \
42| fold -s -w 72
43Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec pretium
44odio quis nisi vestibulum, at semper magna ornare. Nulla facilisi. Sed
45in magna lacus. Proin faucibus est non ligula vehicula, quis ultrices
46lectus ultricies. Aenean sit amet dignissim mauris. Sed luctus
47lobortis augue nec aliquet. Cras in felis tellus. Curabitur id purus
48feugiat enim posuere ultrices in viverra erat. Nulla facilisi. Donec et
49neque hendrerit, dignissim ipsum id, venenatis enim.
50```
51
52As with
53[`rev`](../2024-03-27-rev) and [`cut`](../2024-03-28-cut), the environment
54variable `LC_CTYPE` is used to determine what a character is, but
55one can also specify to use *bytes* instead using the `-b` option.
56
57## Examples
58
59### Text formatting
60
61A classic use for `fold` is formatting emails (or markdown files
62for 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
69will format the next paragraph. However,
70[`fmt`](http://man.openbsd.org/OpenBSD-7.4/fmt) can do the same and it is
71more convenient to use - check it out!
72
73### Generating passwords
74
75Here is a cool example that puts together `fold` with some of the
76other tools we have seen, [`tr`](../2024-01-13-tr) and
77[`head`](../2024-02-20-head-and-tail). To generate a random password,
78I use:
79
80```
81$ cat /dev/random | tr -cd 'a-z0-9' | fold -w 12 | head -1
82```
83
84This command reads the special file `/dev/random`, which contains a
85never-ending stream of random bytes, and passes it through various commands
86in a *pipeline*. First, every character that is *not* (`tr -c`) a
87lowercase letter or a number (`a-z0-9`) is deleted (`-d`); then
88the result is `fold`'d to 12 characters (`fold -w 12`); finally,
89the first line is taken (`head -1`).
diff --git a/src/series/series.md b/src/series/series.md
index d2df0af..a28ea04 100644
--- a/src/series/series.md
+++ b/src/series/series.md
@@ -34,6 +34,7 @@ of complexity: `grep`, `sed` and `awk`. Work in progress.
34* Part 2.3: [rev](../blog/2024-03-27-rev) 34* Part 2.3: [rev](../blog/2024-03-27-rev)
35* Part 2.4: [cut](../blog/2024-03-28-cut) 35* Part 2.4: [cut](../blog/2024-03-28-cut)
36* Part 2.5: [expand and unexpand](../blog/2024-04-07-expand-unexpand) 36* Part 2.5: [expand and unexpand](../blog/2024-04-07-expand-unexpand)
37* Part 2.6: [fold](../blog/2024-05-31-fold)
37* Part 3: awk (coming "soon") 38* Part 3: awk (coming "soon")
38 39
39## The UNIX shell as an IDE 40## The UNIX shell as an IDE

Generated with cgit - Back to sebastiano.tronto.net