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

commit d1504905f459494c99bccb3be17f6f70258e1e0c
parent 57056f658bbba0ea1b3dd9fece8dc00c17e2dade
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Sun,  7 Apr 2024 07:01:58 +0200

added blog post

Diffstat:
Msrc/blog/2024-03-28-cut/cut.md | 2++
Asrc/blog/2024-04-07-expand-unexpand/expand-unexpand.md | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/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 @@ -136,3 +136,5 @@ something more complicated with the data than just printing it out. For this reason I have always relied on more complete languages, like C or Python, rather than shell scripting. But `cut` is definitely a convenient tool to be familiar with, given how simple it is! + +*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 @@ -0,0 +1,58 @@ +# UNIX text filters, part 2.5 of of 3: expand and unexpand + +*This post is part of a [series](../../series)* + +Last week some guys at my company gave a little lecture on +[rust](https://www.rust-lang.org). They gave us some practical exercises, +with some partially-written source files to complete. Unfortunately, the +source files used spaces instead of TABs! + +Luckily I knew how to use `unexpand` to fix this. And in case you +are shaking your head in horror because you prefer spaces, worry +not: `expand` can save you when you face the opposite problem. + +## expand + +The `expand` command converts tabs into spaces. By default every +TAB character *at the beginning of each line* is replaced by 8 +spaces, but you can choose a different width with the `-t` option. +You can also choose to expand *all* TABs, not just the leading ones, +using the `-a` option. For example: + +``` +$ echo ' hello world?' | expand -t 3 + hello world? +``` + +As you can see, the TAB in the middle of the line si preserved. +Well I guess you can't really see it. Also, I am pretty sure the +markdown-to-html converter I use turns tabs into spaces anyway. +This example is a trainwreck, let's move on. + +## unexpand + +Unsurprisingly, `unexpand` does just the opposite of `expand`: it +converts groups of leading spaces into TABs. So the command I used +to "fix" the source files for the rust exercises was: + +``` +$ unexpand -t 4 main.rs +``` + +or to be more precise, since I wanted to replace the original file: + +``` +$ unexpand -t 4 main.rs > main.rs.2 && mv main.rs.2 main.rs +``` + +or to be even more precise, since I wanted to do this for multiple files: + +``` +$ for f in *.rs; do unexpand $f > $f.2 && mv $f.2 $f; done +``` + +## Quirks + +For some reason, the OpenBSD version of `unexpand` does not allow +using the `-t` option. So if I had brought my OpenBSD laptop at the +rust lecture I would have been stuck with spaces :( diff --git a/src/series/series.md b/src/series/series.md @@ -33,6 +33,7 @@ of complexity: `grep`, `sed` and `awk`. Work in progress. * Part 2.2: [head and tail](../blog/2024-02-20-head-and-tail) * Part 2.3: [rev](../blog/2024-03-27-rev) * Part 2.4: [cut](../blog/2024-03-28-cut) +* Part 2.5: [expand and unexpand](../blog/2024-04-07-expand-unexpand) * Part 3: awk (coming "soon") ## The UNIX shell as an IDE