diff options
| Commit | d6fba71256111e93326a00fb56d1b5428f8253ce (patch) | |
|---|---|---|
| Author | 2023-04-01 17:03:31 +0300 | |
| Committer | 2023-04-01 17:03:31 +0300 | |
| Files | 4f640aa95fc992d09b14e845d0953d90e3fea56e | |
| syndamia.com-d6fba71256111e93326a00fb56d1b5428f8253ce.tar syndamia.com-d6fba71256111e93326a00fb56d1b5428f8253ce.tar.gz syndamia.com-d6fba71256111e93326a00fb56d1b5428f8253ce.zip | ||
| Parent | 1eaeb63af578fe4b53847e23f5677095b0851bda (diff) | |
[#strings] Added custom awk library
1 files changed, 45 insertions, 0 deletions
| -rw-r--r-- | awk-edits/#strings.awk | 45 |
diff --git a/awk-edits/#strings.awk b/awk-edits/#strings.awk new file mode 100644 index 0000000..a34757e --- /dev/null +++ b/awk-edits/#strings.awk @@ -0,0 +1,45 @@ +# gosub(regexp, replacement [, target]) +# Aka global original sub +# Behaves like gsub, however doesn't try regexp on previoud replacements +# and returns the result string, rather than doing it inplace. +function gosub(regexp, replacement, target) { + if (!length(target)) { + target = $0; + } + + result = ""; + matched = ""; + while (match(target, regexp)) { + result = result substr(target, 1, RSTART - 1); + matched = substr(target, RSTART, RLENGTH); + target = substr(target, RSTART + RLENGTH); + + sub(regexp, replacement, matched); + result = result matched; + } + + return result target; +} + +# gesub(regexp, exregexp, replacement [, target]) +# Aka global excluding sub +# Behaves like gsub, however substitutes only where exregexp doesn't match +# and returns the result string, rather than doing it inplace. +function gesub(regexp, exregexp, replacement, target) { + if (!length(target)) { + target = $0; + } + + result = ""; + while (match(target, exregexp)) { + beg = substr(target, 1, RSTART - 1); + exclude = substr(target, RSTART, RLENGTH); + target = substr(target, RSTART + RLENGTH); + gsub(regexp, replacement, beg); + result = result beg exclude; + } + + gsub(regexp, replacement, target); + + return result target; +} |