diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2017-05-24 16:56:00 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2017-05-24 16:56:00 +1200 |
commit | cadc05f5acb0b495b93b15981a404077e0d2dee3 (patch) | |
tree | 085529d29b42d0a5605dcd57225cac7a95c4e0e9 | |
parent | Exit 2 with usage errors from gwp(1df) (diff) | |
download | dotfiles-cadc05f5acb0b495b93b15981a404077e0d2dee3.tar.gz dotfiles-cadc05f5acb0b495b93b15981a404077e0d2dee3.zip |
Add trs(1df)
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | README.markdown | 1 | ||||
-rw-r--r-- | bin/trs.awk | 36 | ||||
-rw-r--r-- | man/man1/trs.1df | 23 |
5 files changed, 62 insertions, 0 deletions
@@ -104,6 +104,7 @@ bin/tl bin/tlcs bin/tm bin/tot +bin/trs bin/try bin/u2d bin/umake @@ -177,6 +177,7 @@ BINS = bin/ap \ bin/tlcs \ bin/tm \ bin/tot \ + bin/trs \ bin/try \ bin/u2d \ bin/umake \ diff --git a/README.markdown b/README.markdown index 1738af22..4cd8ebd0 100644 --- a/README.markdown +++ b/README.markdown @@ -532,6 +532,7 @@ Installed by the `install-bin` target: to use Taskwarrior, but found it too complex and buggy. * `tm(1df)` runs `tmux(1)` with `attach-session -d` if a session exists, and `new-session` if it doesn't. +* `trs(1df)` replaces strings (not regular expression) in its input. * `try(1df)` repeats a command up to a given number of times until it succeeds, only printing error output if all three attempts failed. Good for tolerating blips or temporary failures in `cron(8)` scripts. diff --git a/bin/trs.awk b/bin/trs.awk new file mode 100644 index 00000000..5966c520 --- /dev/null +++ b/bin/trs.awk @@ -0,0 +1,36 @@ +# Substitute one string for another in input (no regex) +BEGIN { + # Name self + self = "trs" + + # No wordsplitting required + FS = "" + + # Two and only two arguments required + if (ARGC != 3) + fail("Need a string and a replacement") + + # Get arguments and blank them so awk doesn't try to read them as files + str = ARGV[1] + rep = ARGV[2] + ARGV[1] = ARGV[2] = "" + + # String length is relevant here + if (!(len = length(str))) + fail("String to replace cannot be null") +} + +# Bailout function +function fail(str) { + printf "%s: %s\n", self, str | "cat >&2" + exit(2) +} + +# Run on each line +{ + lin = "" + for (buf = $0; ind = index(buf, str); buf = substr(buf, ind + len)) + lin = lin substr(buf, 1, ind - 1) rep + lin = lin buf + print lin +} diff --git a/man/man1/trs.1df b/man/man1/trs.1df new file mode 100644 index 00000000..fa5d2d19 --- /dev/null +++ b/man/man1/trs.1df @@ -0,0 +1,23 @@ +.TH TRS 1df "May 2017" "Manual page for trs" +.SH NAME +.B trs +\- string version of tr(1) +.SH SYNOPSIS +.B trs +STRING REPLACEMENT +< file +.br +program | +.B trs +STRING REPLACEMENT +.SH DESCRIPTION +.B trs +replaces the string given in its first argument with the string given in its +second, with no regex metacharacters, in a way that should work on all POSIX +implementations. It is thereby the string complement for tr(1). +.P +The first argument cannot be a null string. The second argument can be blank +(but must still be specified) to implicitly delete all occurrences of the +string. +.SH AUTHOR +Tom Ryder <tom@sanctum.geek.nz> |