aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/blog/2023-12-03-sed/sed.md4
-rw-r--r--src/blog/2024-01-13-tr/tr.md109
-rw-r--r--src/series/series.md1
3 files changed, 113 insertions, 1 deletions
diff --git a/src/blog/2023-12-03-sed/sed.md b/src/blog/2023-12-03-sed/sed.md
index c422879..4d50ed9 100644
--- a/src/blog/2023-12-03-sed/sed.md
+++ b/src/blog/2023-12-03-sed/sed.md
@@ -399,4 +399,6 @@ to take a small detour and talk about some other simple, special-purpose
399text filtering commands, such as `tr`, `head`, `fmt` and so on. Expect 399text filtering commands, such as `tr`, `head`, `fmt` and so on. Expect
400some short posts in this series before part 3 - after all, there are 400some short posts in this series before part 3 - after all, there are
401[uncountably many](https://en.wikipedia.org/wiki/Uncountable_set) 401[uncountably many](https://en.wikipedia.org/wiki/Uncountable_set)
402numbers between two and 3! 402numbers between 2 and 3!
403
404*Next in the series: [tr](../2024-01-13-tr)*
diff --git a/src/blog/2024-01-13-tr/tr.md b/src/blog/2024-01-13-tr/tr.md
new file mode 100644
index 0000000..207fde4
--- /dev/null
+++ b/src/blog/2024-01-13-tr/tr.md
@@ -0,0 +1,109 @@
1# UNIX text filters, part 2.1 of 3: tr
2
3*This post is part of a [series](../../series)*
4
5In the [post about sed](../2023-12-03-sed) I have not discussed the
6`y` command at all. This is because I realized it is just an
7underpowered version of the `tr` command, that we are going to
8explore in this post.
9
10The `tr` command is a simple utility that can perform
11character-by-character substitutions and a couple of other things.
12Like most UNIX utilities, it operates on standard input and standard
13output by default.
14
15## Replacing
16
17The most basic form of a `tr` command is
18
19```
20$ tr string1 string2
21```
22
23which replaces every occurrence of a character present in `string1`
24with its corresponding character in `string2` - the first with the
25first, the second with the second and so on. If `string2` is shorter
26than `string1`, the last character is repeated as needed.
27
28For example
29
30```
31$ echo 'Hello!' | tr le 13
32H311o!
33```
34
35An equivalent `sed` command would be `sed 'y/le/13/'`.
36
37Like sed and [grep](../2023-08-20-grep), also `tr` supports the
38standard character sets like `[:upper:]`, `[:alpha:]` and so on.
39For example, the following command capitalizes every letter in the
40input string:
41
42```
43$ echo 'Hello!' | tr [:lower:] [:upper:]
44HELLO!
45```
46
47## Deleting
48
49With the `-d` option one can delete characters:
50
51```
52$ echo 'R42emo3vin0g all n66umber3s!' | tr -d 0-9
53Removing all numbers!
54```
55
56Here I have used the *character range* `0-9` instead of `[:digit:]`.
57Other examples of valid character ranges are `A-Z`, `0-8` and `a-f`.
58
59The `-d` option can be combined with the `-c` option, which takes
60the *complement* of a given set of characters:
61
62```
63$ echo 'R42emo3vin0g all non-n66umber3s!' | tr -cd '0-9\n'
644230663
65```
66
67Notice that I have also added `\n` to our list of
68characters, so that the newline at the end of the text is kept.
69
70A more complex example involving `tr -cd` is the following, which
71I use to generate random passowrds:
72
73```
74$ cat /dev/random | tr -cd 'a-z0-9' | fold -w 12 | head -n 1
75ft82mtfsy5ps
76```
77
78Here `/dev/random` spits out random data, while the commands
79`fold -w 12` and `head -n 1` are used to break the input text into
80lines of 12 characters and take the first line of the input,
81respectively. We'll talk about them in future posts.
82
83## Squeezing
84
85One more thing `tr` can do is *squeezing* consecutive identical
86characters. For example:
87
88```
89$ echo Helllllo | tr -s l
90Helo
91```
92
93The `-s` option can be combined with the `-c` or `-d` option, and
94in this case the squeezing is performed last, squeezing all the
95characters contained in the last given string:
96
97```
98$ echo 'Hellllo! 112233' | tr -s 'l_e' '123'
99H31o! 123
100$ echo 'Hello!' | tr -ds '!' 'l'
101Helo
102```
103
104## Conclusions
105
106This is pretty much all there is tu say about `tr`. All of this can
107probably be done with a sufficiently complicated `sed` or `awk`
108script, but it is definitely nice to have a simpler utility to perform
109easy changes.
diff --git a/src/series/series.md b/src/series/series.md
index 34f34d7..216c105 100644
--- a/src/series/series.md
+++ b/src/series/series.md
@@ -29,6 +29,7 @@ of complexity: `grep`, `sed` and `awk`. Work in progress.
29* Part 0: [Regular expressions](../blog/2023-06-16-regex) 29* Part 0: [Regular expressions](../blog/2023-06-16-regex)
30* Part 1: [grep](../blog/2023-08-20-grep) 30* Part 1: [grep](../blog/2023-08-20-grep)
31* Part 2: [sed](../blog/2023-12-03-sed) 31* Part 2: [sed](../blog/2023-12-03-sed)
32* Part 2.1: [tr](../blog/2024-01-13-tr)
32* Part 3: awk (coming "soon") 33* Part 3: awk (coming "soon")
33 34
34## The UNIX shell as an IDE 35## The UNIX shell as an IDE

Generated with cgit - Back to sebastiano.tronto.net