diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-04-07 07:01:58 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-04-07 07:01:58 +0200 |
| commit | d1504905f459494c99bccb3be17f6f70258e1e0c (patch) | |
| tree | 6bc6a0b6f5afce4624746b535d00b7edeb2d1697 | |
| parent | 57056f658bbba0ea1b3dd9fece8dc00c17e2dade (diff) | |
| download | sebastiano.tronto.net-d1504905f459494c99bccb3be17f6f70258e1e0c.tar.gz sebastiano.tronto.net-d1504905f459494c99bccb3be17f6f70258e1e0c.zip | |
added blog post
| -rw-r--r-- | src/blog/2024-03-28-cut/cut.md | 2 | ||||
| -rw-r--r-- | src/blog/2024-04-07-expand-unexpand/expand-unexpand.md | 58 | ||||
| -rw-r--r-- | src/series/series.md | 1 |
3 files changed, 61 insertions, 0 deletions
diff --git a/src/blog/2024-03-28-cut/cut.md b/src/blog/2024-03-28-cut/cut.md index 4e0677d..719efa2 100644 --- a/src/blog/2024-03-28-cut/cut.md +++ b/src/blog/2024-03-28-cut/cut.md | |||
| @@ -136,3 +136,5 @@ something more complicated with the data than just printing it out. | |||
| 136 | For this reason I have always relied on more complete languages, | 136 | For this reason I have always relied on more complete languages, |
| 137 | like C or Python, rather than shell scripting. But `cut` is definitely | 137 | like C or Python, rather than shell scripting. But `cut` is definitely |
| 138 | a convenient tool to be familiar with, given how simple it is! | 138 | a convenient tool to be familiar with, given how simple it is! |
| 139 | |||
| 140 | *Next in the series: [expand and unexpand](../2024-04-07-expand-unexpand)* | ||
diff --git a/src/blog/2024-04-07-expand-unexpand/expand-unexpand.md b/src/blog/2024-04-07-expand-unexpand/expand-unexpand.md new file mode 100644 index 0000000..4bc1c04 --- /dev/null +++ b/src/blog/2024-04-07-expand-unexpand/expand-unexpand.md | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | # UNIX text filters, part 2.5 of of 3: expand and unexpand | ||
| 2 | |||
| 3 | *This post is part of a [series](../../series)* | ||
| 4 | |||
| 5 | Last week some guys at my company gave a little lecture on | ||
| 6 | [rust](https://www.rust-lang.org). They gave us some practical exercises, | ||
| 7 | with some partially-written source files to complete. Unfortunately, the | ||
| 8 | source files used spaces instead of TABs! | ||
| 9 | |||
| 10 | Luckily I knew how to use `unexpand` to fix this. And in case you | ||
| 11 | are shaking your head in horror because you prefer spaces, worry | ||
| 12 | not: `expand` can save you when you face the opposite problem. | ||
| 13 | |||
| 14 | ## expand | ||
| 15 | |||
| 16 | The `expand` command converts tabs into spaces. By default every | ||
| 17 | TAB character *at the beginning of each line* is replaced by 8 | ||
| 18 | spaces, but you can choose a different width with the `-t` option. | ||
| 19 | You can also choose to expand *all* TABs, not just the leading ones, | ||
| 20 | using the `-a` option. For example: | ||
| 21 | |||
| 22 | ``` | ||
| 23 | $ echo ' hello world?' | expand -t 3 | ||
| 24 | hello world? | ||
| 25 | ``` | ||
| 26 | |||
| 27 | As you can see, the TAB in the middle of the line si preserved. | ||
| 28 | Well I guess you can't really see it. Also, I am pretty sure the | ||
| 29 | markdown-to-html converter I use turns tabs into spaces anyway. | ||
| 30 | This example is a trainwreck, let's move on. | ||
| 31 | |||
| 32 | ## unexpand | ||
| 33 | |||
| 34 | Unsurprisingly, `unexpand` does just the opposite of `expand`: it | ||
| 35 | converts groups of leading spaces into TABs. So the command I used | ||
| 36 | to "fix" the source files for the rust exercises was: | ||
| 37 | |||
| 38 | ``` | ||
| 39 | $ unexpand -t 4 main.rs | ||
| 40 | ``` | ||
| 41 | |||
| 42 | or to be more precise, since I wanted to replace the original file: | ||
| 43 | |||
| 44 | ``` | ||
| 45 | $ unexpand -t 4 main.rs > main.rs.2 && mv main.rs.2 main.rs | ||
| 46 | ``` | ||
| 47 | |||
| 48 | or to be even more precise, since I wanted to do this for multiple files: | ||
| 49 | |||
| 50 | ``` | ||
| 51 | $ for f in *.rs; do unexpand $f > $f.2 && mv $f.2 $f; done | ||
| 52 | ``` | ||
| 53 | |||
| 54 | ## Quirks | ||
| 55 | |||
| 56 | For some reason, the OpenBSD version of `unexpand` does not allow | ||
| 57 | using the `-t` option. So if I had brought my OpenBSD laptop at the | ||
| 58 | rust lecture I would have been stuck with spaces :( | ||
diff --git a/src/series/series.md b/src/series/series.md index eece440..d2df0af 100644 --- a/src/series/series.md +++ b/src/series/series.md | |||
| @@ -33,6 +33,7 @@ of complexity: `grep`, `sed` and `awk`. Work in progress. | |||
| 33 | * Part 2.2: [head and tail](../blog/2024-02-20-head-and-tail) | 33 | * Part 2.2: [head and tail](../blog/2024-02-20-head-and-tail) |
| 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 3: awk (coming "soon") | 37 | * Part 3: awk (coming "soon") |
| 37 | 38 | ||
| 38 | ## The UNIX shell as an IDE | 39 | ## The UNIX shell as an IDE |
