{"id":18925561,"url":"https://github.com/nightmachinery/prefixer","last_synced_at":"2026-03-14T15:30:20.145Z","repository":{"id":53153351,"uuid":"292936298","full_name":"NightMachinery/prefixer","owner":"NightMachinery","description":"Manipulate records stored in a single string read from stdin.","archived":false,"fork":false,"pushed_at":"2023-12-15T08:22:28.000Z","size":28,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-31T17:48:04.866Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NightMachinery.png","metadata":{"files":{"readme":"readme.org","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-04T20:02:54.000Z","updated_at":"2021-09-22T14:02:15.000Z","dependencies_parsed_at":"2024-11-08T11:22:26.077Z","dependency_job_id":null,"html_url":"https://github.com/NightMachinery/prefixer","commit_stats":null,"previous_names":["nightmachinary/prefixer"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NightMachinery%2Fprefixer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NightMachinery%2Fprefixer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NightMachinery%2Fprefixer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NightMachinery%2Fprefixer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NightMachinery","download_url":"https://codeload.github.com/NightMachinery/prefixer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239921844,"owners_count":19718844,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T11:12:21.489Z","updated_at":"2026-03-14T15:30:20.081Z","avatar_url":"https://github.com/NightMachinery.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"#+TITLE: Prefixer.go\n\n* Installation\n#+begin_example zsh\ngo get -u -v github.com/NightMachinary/prefixer\n#+end_example\n\n* Usage\n** Manual\n#+BEGIN_SRC bash :results verbatim :exports both\nprefixer --help\n#+END_SRC\n\n#+RESULTS:\n#+begin_example\nPrefixer is a general tool that allows you to manipulate records stored in a string format.\n\nThe string '\\x00' will be converted to the null character in \u003cadd-prefix\u003e, \u003cadd-postfix\u003e, \u003crm-prefix\u003e, \u003cisep\u003e, and \u003cosep\u003e. (No escape mechanism has been implemented for them yet.)\nThis is because there does not seem to be a way to pass this character as an argument on the OS level.\n\nThe separators are by default the newline character '\\n'.\n\nWhen tracking is enabled, the magic string 'PREFIXER_LINENUMBER' in \u003cadd-prefix\u003e, \u003cadd-postfix\u003e, and \u003creplace\u003e will be replaced with the line number of the current record.\n\nUsage:\n  prefixer [options]\n  prefixer rm [options --] [\u003crecord\u003e...]\n  prefixer replace [options --] [(\u003cfrom\u003e \u003cto\u003e)...]\n  prefixer -h | --help\n\n  rm: will skip the records supplied (i.e., remove those records from the output). This happens after potentially trimming the record.\n\nOptions:\n  --tac  Reverses the order of output records, ala GNU tac.\n  -a --add-prefix=\u003cadd-prefix\u003e  Adds this prefix to the beginning of each record.\n  -p --add-postfix=\u003cadd-postfix\u003e  Adds this to the end of each record.\n  -c --case-sensitivity=\u003ccase-sensitivity\u003e  Sets the case sensitivity for --remove-prefix: no, yes. (Default: yes)\n  -r --remove-prefix=\u003crm-prefix\u003e  Removes this prefix from the beginning of each record.\n  -s --skip-empty  Skip empty records (after the removal of --remove-prefix).\n  -i --input-sep=\u003cisep\u003e  Input record separator.\n  -o --output-sep=\u003cosep\u003e  Output record separator.\n  -t --trim  Trims whitespace from around each record before other transformations have been done.\n  --process-include=\u003cprocess-include\u003e  Ranges of the input records to process. This uses fzf's range syntax. Unprocessed records will be output as they are.\n  -x --included-only  Exclude everything that is included by --process-include.\n  --rm-ansi  Strip the ANSI color codes from input records when testing for equality in rm or replace.\n  --rm-x  Enable \\x00 to NUL conversion for \u003crecord\u003e.\n  --from-x  Enable \\x00 to NUL conversion for \u003cfrom\u003e.\n  --to-x  Enable \\x00 to NUL conversion for \u003cto\u003e.\n  --replace=\u003creplace\u003e  Replace all records without matches in \u003cfrom\u003e records with \u003creplace\u003e. '$1' will be expanded to the original record.\n  -l --location=\u003cloc-file\u003e  Enables tracking the starting line number of each record, and prints those numbers to the supplied file (separated by newlines). Use /dev/null to just enable the tracking.\n  -h --help  Show this screen.\n#+end_example\n\n** Examples\n\n#+begin_src bash :results verbatim :exports both\necho \"Green Paint, Red Paint, Yellow Paint\" | prefixer -i \", \" -r \"Green \" -a \"Cute \"\n#+end_src\n\n#+RESULTS:\n#+begin_example\nCute Paint\nCute Red Paint\nCute Yellow Paint\n#+end_example\n\n#+begin_src bsh.dash :results verbatim :exports both :wrap example\necho \"Green Paint, Red Paint, Yellow Paint\" | prefixer --case-sensitivity no -i \", \" -r \"grEEn \" -a \"Cute \"\n#+end_src\n\n#+RESULTS:\n#+begin_example\nCute Paint\nCute Red Paint\nCute Yellow Paint\n#+end_example\n\n#+begin_src bash :results verbatim :exports both\necho \"Green Paint, Red Paint, Yellow Paint\" | prefixer -i \", \" -o $'\\n---\\n' -r \"Green \" -a \"PREFIXER_LINENUMBER: Cute \" -l /dev/null\n#+end_src\n\n#+RESULTS:\n#+begin_example\n1: Cute Paint\n---\n1: Cute Red Paint\n---\n1: Cute Yellow Paint\n#+end_example\n\n#+begin_src bash :results verbatim :exports both\necho \"Green Paint\n\nRed Paint\n\n     Yellow Paint\" | prefixer -i $'\\n' -o $'\\n' -r \"Green \" -a \"PREFIXER_LINENUMBER: Cute \" -l /dev/null\n#+end_src\n\n#+RESULTS:\n#+begin_example\n1: Cute Paint\n2: Cute\n3: Cute Red Paint\n4: Cute\n5: Cute      Yellow Paint\n6: Cute\n#+end_example\n\n#+begin_src bash :results verbatim :exports both\necho \"Green Paint\n\nRed Paint\n\n     Yellow Paint\" | prefixer --skip-empty -i $'\\n' -o $'\\n' -r \"Green \" -a \"PREFIXER_LINENUMBER: Cute \" -l /tmp/locationData\n#+end_src\n\n#+RESULTS:\n#+begin_example\n1: Cute Paint\n3: Cute Red Paint\n5: Cute      Yellow Paint\n#+end_example\n\n#+begin_src bash :results verbatim :exports both\ncat /tmp/locationData\n#+end_src\n\n#+RESULTS:\n#+begin_example\n1\n3\n5\n#+end_example\n\n\n#+begin_src bash :results verbatim :exports both\necho \"Green Paint\n\nRed Paint\n\n     Yellow Paint\" | prefixer --tac --skip-empty -i $'\\n' -o $'\\n' -r \"Green \" -a \"PREFIXER_LINENUMBER: Cute \" -l /tmp/locationData\n#+end_src\n\n#+RESULTS:\n#+begin_example\n5: Cute      Yellow Paint\n3: Cute Red Paint\n1: Cute Paint\n#+end_example\n\n#+begin_src bash :results verbatim :exports both\necho \"a,  snake ,d\" | prefixer rm -i , -o '-' --trim -r sn a ake  -a \"hi \"\n#+end_src\n\n#+RESULTS:\n#+begin_example\nhi d\n#+end_example\n\n#+begin_src bash :results verbatim :exports both\necho \"a,  snake ,d\" | prefixer rm -i , -o '-' --trim -r sn -- d  -a \"hi \"\n#+end_src\n\n#+RESULTS:\n#+begin_example\na-ake\n#+end_example\n\n#+begin_src bash :results verbatim :exports both\necho \"a,  snake ,d\" | prefixer rm -i , -o '-' -r sn -- d  -a \"hi \"\n#+end_src\n\n#+RESULTS:\n#+begin_example\na-  snake -d\n#+end_example\n\nLet's define some helper functions:\n\n#+begin_src bsh.dash :results verbatim :exports both :wrap example\narrN () {\n    print -nr -- \"${(pj.\\n.)@}\"\n}\narrN \"This writes its args in new lines.\" \"See?\" \"1 2 3\"\n#+end_src\n\n#+RESULTS:\n#+begin_example\nThis writes its args in new lines.\nSee?\n1 2 3\n#+end_example\n\n#+begin_src bsh.dash :results verbatim :exports both :wrap example\narr0 () {\n    print -nr -- \"${(pj.\\0.)@}\"\n}\n\narr0 \"This writes its args between NUL chars.\" \"See?\" \"1 2 3\" | cat -v\n#+end_src\n\n#+RESULTS:\n#+begin_example\nThis writes its args between NUL chars.^@See?^@1 2 3\n#+end_example\n\n#+begin_src bsh.dash :results verbatim :exports both :wrap example\narrN red orange yellow green blue purple gray black aqua teal | prefixer --process-include 2..-4,-1 --add-prefix '\u003c' --add-postfix '\u003e'\n#+end_src\n\n#+RESULTS:\n#+begin_example\nred\n\u003corange\u003e\n\u003cyellow\u003e\n\u003cgreen\u003e\n\u003cblue\u003e\n\u003cpurple\u003e\n\u003cgray\u003e\nblack\naqua\n\u003cteal\u003e\n#+end_example\n\n#+begin_src bsh.dash :results verbatim :exports both :wrap example\narrN \"fish\" \"green mold\" \"green\" \"yellow mold\" \"daddy\" | prefixer replace --skip-empty --remove-prefix 'yellow ' --add-prefix 'hi, ' --add-postfix '!' -- \"fish\" '' \"green\" red mold wolf\n#+end_src\n\n#+RESULTS:\n#+begin_example\nhi, green mold!\nhi, red!\nhi, wolf!\nhi, daddy!\n#+end_example\n\n#+begin_src bsh.dash :results verbatim :exports both :wrap example\narr0 \"fish\" \"green mold\" \"green\" \"yellow mold\" \"daddy\" | prefixer -i '\\x00' replace --tac --skip-empty --remove-prefix 'yellow ' --add-prefix 'hi, ' --add-postfix '!' -- \"fish\" '' \"green\" red mold wolf | cat -v\n#+end_src\n\n#+RESULTS:\n#+begin_example\nhi, daddy!\nhi, wolf!\nhi, red!\nhi, green mold!\n#+end_example\n\nNote that in the following example, the last record is =IV\\n= and not =IV=, and so it is not replaced.\n#+begin_src bsh.dash :results verbatim :exports both :wrap example\necho a IV b c IV | prefixer replace -i ' ' -o ' ' IV 4\n#+end_src\n\n#+RESULTS:\n#+begin_example\na 4 b c IV\n#+end_example\n\n*** Search and replace literal strings\n#+begin_src bash :results verbatim :exports both\nfunction replace() {\n    prefixer -i \"$1\" -o \"$2\"\n}\nreplace wolf \"cute rabbit\" \u003c\u003c \"EOF\"\nThe wolf (Canis lupus[a]), also known as the gray wolf or grey wolf, is a large canine native to Eurasia and North America. More than thirty subspecies of Canis lupus have been recognized, and gray wolves, as colloquially understood, comprise non-domestic/feral subspecies.\nEOF\n#+end_src\n\n#+RESULTS:\n#+begin_example\nThe cute rabbit (Canis lupus[a]), also known as the gray cute rabbit or grey cute rabbit, is a large canine native to Eurasia and North America. More than thirty subspecies of Canis lupus have been recognized, and gray wolves, as colloquially understood, comprise non-domestic/feral subspecies.\n#+end_example\n\n*** Test-ish examples\nThese examples are mostly meant as unit tests.\n\n#+begin_src bsh.dash :results verbatim :exports both :wrap example\narrN 1 2 3 a$'\\0'b | prefixer replace --from-x -- 1 9 5 6 1 \"hii\\x00i\\!\" 'a\\x00b' wow | cat -v\n#+end_src\n\n#+RESULTS:\n#+begin_example\nhii\\x00i\\!\n2\n3\nwow\n#+end_example\n\n#+begin_src bsh.dash :results verbatim :exports both :wrap example\narrN 1 2 3 a$'\\0'b | prefixer replace --from-x --to-x -- 1 9 5 6 1 \"hii\\x00i\\!\" 'a\\x00b' wow | cat -v\n#+end_src\n\n#+RESULTS:\n#+begin_example\nhii^@i\\!\n2\n3\nwow\n#+end_example\n\n#+begin_src bsh.dash :results verbatim :exports both :wrap example\narrN 1 2 3 a$'\\0'b | prefixer replace --to-x -- 1 9 5 6 1 \"hii\\x00i\\!\" 'a\\x00b' wow | cat -v\n#+end_src\n\n#+RESULTS:\n#+begin_example\nhii^@i\\!\n2\n3\na^@b\n#+end_example\n\n#+begin_src bsh.dash :results verbatim :exports both :wrap example\narrN 1 2 3 a$'\\0'b | prefixer replace -- 1 9 5 6 1 \"hii\\x00i\\!\" 'a\\x00b' wow | cat -v\n#+end_src\n\n#+RESULTS:\n#+begin_example\nhii\\x00i\\!\n2\n3\na^@b\n#+end_example\n\n* Benchmarks\n\n\n#+begin_src bsh.dash :results verbatim :exports both\nhyperfine --warmup 10  \"\u003c$attic sd \"$'\\36'\" H\" \"\u003c$attic tr \"$'\\36'\" H\" \"\u003c$attic prefixer -s -i \"$'\\36'\" -o 'H' \"\n#+end_src\n\n#+RESULTS:\n#+begin_example\nBenchmark #1: \u003c/Users/evar/cellar/attic//.darkattic sd \u001e H\n  Time (mean ± σ):       8.3 ms ±   8.2 ms    [User: 2.0 ms, System: 2.6 ms]\n  Range (min … max):     2.3 ms …  63.9 ms    140 runs\n\n  Warning: Command took less than 5 ms to complete. Results might be inaccurate.\n  Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet PC without any interferences from other programs. It might help to use the '--warmup' or '--prepare' options.\n\nBenchmark #2: \u003c/Users/evar/cellar/attic//.darkattic tr \u001e H\n  Time (mean ± σ):       8.5 ms ±   7.8 ms    [User: 3.6 ms, System: 2.7 ms]\n  Range (min … max):     3.3 ms …  49.1 ms    141 runs\n\n  Warning: Command took less than 5 ms to complete. Results might be inaccurate.\n  Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet PC without any interferences from other programs. It might help to use the '--warmup' or '--prepare' options.\n\nBenchmark #3: \u003c/Users/evar/cellar/attic//.darkattic prefixer -s -i \u001e -o 'H'\n  Time (mean ± σ):      11.9 ms ±   5.3 ms    [User: 5.4 ms, System: 3.9 ms]\n  Range (min … max):     6.4 ms …  37.4 ms    120 runs\n\nSummary\n  '\u003c/Users/evar/cellar/attic//.darkattic sd \u001e H' ran\n    1.03 ± 1.38 times faster than '\u003c/Users/evar/cellar/attic//.darkattic tr \u001e H'\n    1.43 ± 1.55 times faster than '\u003c/Users/evar/cellar/attic//.darkattic prefixer -s -i \u001e -o 'H' '\n#+end_example\n\n#+begin_src bash :results verbatim :exports both\nhyperfine --warmup 5 \"arrN {1..2000} | prefixer -r 12\" \"arrN {1..2000} | rmprefix 12 '' '\\x00'\" \"arrN {1..2000} | prefixer -r 12 -a 'hi you ' -o '\\x00' -l /dev/null\"\n#+end_src\n\n#+RESULTS:\n#+begin_example\nBenchmark #1: arrN {1..2000} | prefixer -r 12\n  Time (mean ± σ):      42.4 ms ±  63.7 ms    [User: 11.8 ms, System: 6.3 ms]\n  Range (min … max):     4.9 ms … 357.1 ms    83 runs\n\nBenchmark #2: arrN {1..2000} | rmprefix 12 '' '\\x00'\n  Time (mean ± σ):      14.7 ms ±  23.5 ms    [User: 7.4 ms, System: 4.2 ms]\n  Range (min … max):     1.7 ms … 159.0 ms    69 runs\n\nBenchmark #3: arrN {1..2000} | prefixer -r 12 -a 'hi you ' -o '\\x00' -l /dev/null\n  Time (mean ± σ):      10.2 ms ±   5.6 ms    [User: 10.8 ms, System: 5.5 ms]\n  Range (min … max):     4.4 ms …  31.9 ms    119 runs\n\nSummary\n  'arrN {1..2000} | prefixer -r 12 -a 'hi you ' -o '\\x00' -l /dev/null' ran\n    1.45 ± 2.44 times faster than 'arrN {1..2000} | rmprefix 12 '' '\\x00''\n    4.16 ± 6.66 times faster than 'arrN {1..2000} | prefixer -r 12'\n#+end_example\n\n* Licenses\n\nDual-licensed under MIT and GPL v3 or later.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnightmachinery%2Fprefixer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnightmachinery%2Fprefixer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnightmachinery%2Fprefixer/lists"}