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 /bin | |
parent | Exit 2 with usage errors from gwp(1df) (diff) | |
download | dotfiles-cadc05f5acb0b495b93b15981a404077e0d2dee3.tar.gz dotfiles-cadc05f5acb0b495b93b15981a404077e0d2dee3.zip |
Add trs(1df)
Diffstat (limited to 'bin')
-rw-r--r-- | bin/trs.awk | 36 |
1 files changed, 36 insertions, 0 deletions
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 +} |