https://github.com/nightmachinery/prefixer
Manipulate records stored in a single string read from stdin.
https://github.com/nightmachinery/prefixer
Last synced: 3 months ago
JSON representation
Manipulate records stored in a single string read from stdin.
- Host: GitHub
- URL: https://github.com/nightmachinery/prefixer
- Owner: NightMachinery
- Created: 2020-09-04T20:02:54.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T08:22:28.000Z (over 2 years ago)
- Last Synced: 2024-12-31T17:48:04.866Z (over 1 year ago)
- Language: Go
- Homepage:
- Size: 27.3 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: readme.org
Awesome Lists containing this project
README
#+TITLE: Prefixer.go
* Installation
#+begin_example zsh
go get -u -v github.com/NightMachinary/prefixer
#+end_example
* Usage
** Manual
#+BEGIN_SRC bash :results verbatim :exports both
prefixer --help
#+END_SRC
#+RESULTS:
#+begin_example
Prefixer is a general tool that allows you to manipulate records stored in a string format.
The string '\x00' will be converted to the null character in , , , , and . (No escape mechanism has been implemented for them yet.)
This is because there does not seem to be a way to pass this character as an argument on the OS level.
The separators are by default the newline character '\n'.
When tracking is enabled, the magic string 'PREFIXER_LINENUMBER' in , , and will be replaced with the line number of the current record.
Usage:
prefixer [options]
prefixer rm [options --] [...]
prefixer replace [options --] [( )...]
prefixer -h | --help
rm: will skip the records supplied (i.e., remove those records from the output). This happens after potentially trimming the record.
Options:
--tac Reverses the order of output records, ala GNU tac.
-a --add-prefix= Adds this prefix to the beginning of each record.
-p --add-postfix= Adds this to the end of each record.
-c --case-sensitivity= Sets the case sensitivity for --remove-prefix: no, yes. (Default: yes)
-r --remove-prefix= Removes this prefix from the beginning of each record.
-s --skip-empty Skip empty records (after the removal of --remove-prefix).
-i --input-sep= Input record separator.
-o --output-sep= Output record separator.
-t --trim Trims whitespace from around each record before other transformations have been done.
--process-include= Ranges of the input records to process. This uses fzf's range syntax. Unprocessed records will be output as they are.
-x --included-only Exclude everything that is included by --process-include.
--rm-ansi Strip the ANSI color codes from input records when testing for equality in rm or replace.
--rm-x Enable \x00 to NUL conversion for .
--from-x Enable \x00 to NUL conversion for .
--to-x Enable \x00 to NUL conversion for .
--replace= Replace all records without matches in records with . '$1' will be expanded to the original record.
-l --location= 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.
-h --help Show this screen.
#+end_example
** Examples
#+begin_src bash :results verbatim :exports both
echo "Green Paint, Red Paint, Yellow Paint" | prefixer -i ", " -r "Green " -a "Cute "
#+end_src
#+RESULTS:
#+begin_example
Cute Paint
Cute Red Paint
Cute Yellow Paint
#+end_example
#+begin_src bsh.dash :results verbatim :exports both :wrap example
echo "Green Paint, Red Paint, Yellow Paint" | prefixer --case-sensitivity no -i ", " -r "grEEn " -a "Cute "
#+end_src
#+RESULTS:
#+begin_example
Cute Paint
Cute Red Paint
Cute Yellow Paint
#+end_example
#+begin_src bash :results verbatim :exports both
echo "Green Paint, Red Paint, Yellow Paint" | prefixer -i ", " -o $'\n---\n' -r "Green " -a "PREFIXER_LINENUMBER: Cute " -l /dev/null
#+end_src
#+RESULTS:
#+begin_example
1: Cute Paint
---
1: Cute Red Paint
---
1: Cute Yellow Paint
#+end_example
#+begin_src bash :results verbatim :exports both
echo "Green Paint
Red Paint
Yellow Paint" | prefixer -i $'\n' -o $'\n' -r "Green " -a "PREFIXER_LINENUMBER: Cute " -l /dev/null
#+end_src
#+RESULTS:
#+begin_example
1: Cute Paint
2: Cute
3: Cute Red Paint
4: Cute
5: Cute Yellow Paint
6: Cute
#+end_example
#+begin_src bash :results verbatim :exports both
echo "Green Paint
Red Paint
Yellow Paint" | prefixer --skip-empty -i $'\n' -o $'\n' -r "Green " -a "PREFIXER_LINENUMBER: Cute " -l /tmp/locationData
#+end_src
#+RESULTS:
#+begin_example
1: Cute Paint
3: Cute Red Paint
5: Cute Yellow Paint
#+end_example
#+begin_src bash :results verbatim :exports both
cat /tmp/locationData
#+end_src
#+RESULTS:
#+begin_example
1
3
5
#+end_example
#+begin_src bash :results verbatim :exports both
echo "Green Paint
Red Paint
Yellow Paint" | prefixer --tac --skip-empty -i $'\n' -o $'\n' -r "Green " -a "PREFIXER_LINENUMBER: Cute " -l /tmp/locationData
#+end_src
#+RESULTS:
#+begin_example
5: Cute Yellow Paint
3: Cute Red Paint
1: Cute Paint
#+end_example
#+begin_src bash :results verbatim :exports both
echo "a, snake ,d" | prefixer rm -i , -o '-' --trim -r sn a ake -a "hi "
#+end_src
#+RESULTS:
#+begin_example
hi d
#+end_example
#+begin_src bash :results verbatim :exports both
echo "a, snake ,d" | prefixer rm -i , -o '-' --trim -r sn -- d -a "hi "
#+end_src
#+RESULTS:
#+begin_example
a-ake
#+end_example
#+begin_src bash :results verbatim :exports both
echo "a, snake ,d" | prefixer rm -i , -o '-' -r sn -- d -a "hi "
#+end_src
#+RESULTS:
#+begin_example
a- snake -d
#+end_example
Let's define some helper functions:
#+begin_src bsh.dash :results verbatim :exports both :wrap example
arrN () {
print -nr -- "${(pj.\n.)@}"
}
arrN "This writes its args in new lines." "See?" "1 2 3"
#+end_src
#+RESULTS:
#+begin_example
This writes its args in new lines.
See?
1 2 3
#+end_example
#+begin_src bsh.dash :results verbatim :exports both :wrap example
arr0 () {
print -nr -- "${(pj.\0.)@}"
}
arr0 "This writes its args between NUL chars." "See?" "1 2 3" | cat -v
#+end_src
#+RESULTS:
#+begin_example
This writes its args between NUL chars.^@See?^@1 2 3
#+end_example
#+begin_src bsh.dash :results verbatim :exports both :wrap example
arrN red orange yellow green blue purple gray black aqua teal | prefixer --process-include 2..-4,-1 --add-prefix '<' --add-postfix '>'
#+end_src
#+RESULTS:
#+begin_example
red
black
aqua
#+end_example
#+begin_src bsh.dash :results verbatim :exports both :wrap example
arrN "fish" "green mold" "green" "yellow mold" "daddy" | prefixer replace --skip-empty --remove-prefix 'yellow ' --add-prefix 'hi, ' --add-postfix '!' -- "fish" '' "green" red mold wolf
#+end_src
#+RESULTS:
#+begin_example
hi, green mold!
hi, red!
hi, wolf!
hi, daddy!
#+end_example
#+begin_src bsh.dash :results verbatim :exports both :wrap example
arr0 "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
#+end_src
#+RESULTS:
#+begin_example
hi, daddy!
hi, wolf!
hi, red!
hi, green mold!
#+end_example
Note that in the following example, the last record is =IV\n= and not =IV=, and so it is not replaced.
#+begin_src bsh.dash :results verbatim :exports both :wrap example
echo a IV b c IV | prefixer replace -i ' ' -o ' ' IV 4
#+end_src
#+RESULTS:
#+begin_example
a 4 b c IV
#+end_example
*** Search and replace literal strings
#+begin_src bash :results verbatim :exports both
function replace() {
prefixer -i "$1" -o "$2"
}
replace wolf "cute rabbit" << "EOF"
The 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.
EOF
#+end_src
#+RESULTS:
#+begin_example
The 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.
#+end_example
*** Test-ish examples
These examples are mostly meant as unit tests.
#+begin_src bsh.dash :results verbatim :exports both :wrap example
arrN 1 2 3 a$'\0'b | prefixer replace --from-x -- 1 9 5 6 1 "hii\x00i\!" 'a\x00b' wow | cat -v
#+end_src
#+RESULTS:
#+begin_example
hii\x00i\!
2
3
wow
#+end_example
#+begin_src bsh.dash :results verbatim :exports both :wrap example
arrN 1 2 3 a$'\0'b | prefixer replace --from-x --to-x -- 1 9 5 6 1 "hii\x00i\!" 'a\x00b' wow | cat -v
#+end_src
#+RESULTS:
#+begin_example
hii^@i\!
2
3
wow
#+end_example
#+begin_src bsh.dash :results verbatim :exports both :wrap example
arrN 1 2 3 a$'\0'b | prefixer replace --to-x -- 1 9 5 6 1 "hii\x00i\!" 'a\x00b' wow | cat -v
#+end_src
#+RESULTS:
#+begin_example
hii^@i\!
2
3
a^@b
#+end_example
#+begin_src bsh.dash :results verbatim :exports both :wrap example
arrN 1 2 3 a$'\0'b | prefixer replace -- 1 9 5 6 1 "hii\x00i\!" 'a\x00b' wow | cat -v
#+end_src
#+RESULTS:
#+begin_example
hii\x00i\!
2
3
a^@b
#+end_example
* Benchmarks
#+begin_src bsh.dash :results verbatim :exports both
hyperfine --warmup 10 "<$attic sd "$'\36'" H" "<$attic tr "$'\36'" H" "<$attic prefixer -s -i "$'\36'" -o 'H' "
#+end_src
#+RESULTS:
#+begin_example
Benchmark #1: