0.6.0 โข Published 3 years ago
jrep1 v0.6.0
jrep
A new generation grep written in node/javascript.
Think of awk & sed with javascript syntax.
Very useful for bash scripting and devops works. Very handy and extermly flexible.
๐งค Usage
jrep\<filter1> \<filter2> ...
- Each
<filter>- is a string transformation (a map callback)
- is the body of a javascript function with arg
x
- The s are applied sequentially on each line in the piped content.
- In case of exception in any of the s, that line is removed.
๐ฉ Built-in primitives
| primitive | meaning | usage |
|---|---|---|
| โจ RE1 โจ | find/replace regexp | RE1( \<regular expression with parentheses> ) |
| โจ RER โจ | find + custom replace | RER( regexp. , a string with p[1],p[2], ... ) |
โจ๏ธ Example
uname -a | jrep 'x.replace(/a/g, "O")'โ Practical examples
- Extract pids
ps aux|jrep 'x.substring(12,24)' - Extract the filenames in a find command:
find .| jrep '/([^\/]*)$/.exec(x)[1]'- Using env for filter names: Another way to write it is to use bash env vars for filter names. It is will be also more reusable. A more readable notation:
EXTRACT_BASENAME='/([^\/]*)$/.exec(x)[1]'
find / | jrep $EXTRACT_BASENAME | head- The
PSAUX_PIDcan extract the PID from the output of the commonly usedps auxcommand (tested on MacOS). Is can be used to extract other columns
PSAUX_PID='[.../([^ ]*) * (\d*) *(\d+\.\d+) *(\d+\.\d+) *(\d+) *(\d+) *([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+) +(.*)$/.exec(x)].slice(1)[1]'
ps aux | jrep "$PSAUX_PID"- Another usage of
PSAUX_PID:
ps aux | grep -v grep | grep http-server | jrep "$PSAUX_PID" | xargs kill- Emulate
cutcommand:ps aux|jrep 'x.substring(12,24)' - Pairs of pid and their running time:
ps aux|jrep "{time=x.substring(70,78); pid=x.substring(15,24); return pid+':'+time;}"- Sort PIDs based on runing time:
ps aux|jrep "{time=x.substring(70,78); pid=x.substring(15,24); return time + ':' + pid;}"|sort- Replace letters with frog: (such a useful scenario)
uname -a | jrep 'x.replace(/[a-z]/g, "๐ธ")'- Using โจ built-in primitives:
RE1โจ. The following two are equivalent:
ps aux | jrep 'RE1(".*(python.*)")'
ps aux | jrep '/.*(python.*)/.exec(x)[1]'- The following three are equivalent
ps aux | jrep 'RE1("(.*python.*)")'
ps aux | jrep '/(.*python.*)/.exec(x)[1]'
ps aux | grep -e python- Using โจ built-in primitive RER โจ. Replace
pythonin filenames with emojies.
find .. | jrep 'RER("(.*)python(.*)", "p[1]+\"๐๐\"+p[2]")'๐ป Find jrep on npm: jrep1
๐ก Some suggested use cases
- Eliminate usage of obscure aommands such as
awk,sed,perl - Unified solution without usual tools
cut,grep - Replace a matched regular expression with given custom ccombination (See
RER) - Extract part of an RE1 pattern (See
RE1)
๐ค
- ๐ Feel free to send Pull Requests.
- ๐ Feel free to request features.
โจ Pros
- ๐ transform (map) text in linux pipes
- ๐ filter text in linux pipes
- ๐ concise
- ๐ versatile
- ๐ exteremly flexible
- ๐ customisable
- ๐ prebuilt primitive operations
- ๐ Super lightweight
- ๐ Zero npm dependencies
- ๐ Docker version available
Cons
- ๐ Needs
nodeinstalled on the system. ๐ If you don't want to install node, an alternative is to usedocker.
๐ Requirements
- NodeJS (tested on node 12 and node v16.15.0)
๐ป Installation
- ๐ป npm
npm i -g jrep1
# test:
uname -a | jrep 'x.replace("a", "O")'- ๐ป MacOS , Linux
git clone https://github.com/sohale/jrep.git
./jrep/scripts/install-macos.bash- ๐ป npm jrep1 on npm
npm install -g jrep- ๐ฑ yarn
yarn install -g jrep๐ Example Usage
Also see test/e2e-test.bash
- ๐ณ docker No need to install Node
find / | \
docker run -i sohale/jrep:latest \
'/\/([^\/]*\.py)$/.exec(x)[1]'- โจ๏ธ npx
uname -a | npx jrep 'x.replace("a", "O")'- ๐ก bash (on the fly)
find .. | \
node -e "$(curl -L https://raw.githubusercontent.com/sohale/jrep/main/src/jrep.js | tail -n +2))" '' \
'/\/([^\/]*\.py)$/.exec(x)[1]'- requires NodeJS (tested on node 12) to be installed on your system.
๐ป Development โจ๏ธ
๐ Tutorial
(comming soon)