0.6.0 โ€ข Published 2 years ago

jrep1 v0.6.0

Weekly downloads
-
License
GPL
Repository
github
Last release
2 years ago

jrep

Version CircleCI Downloads/week License

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

primitivemeaningusage
โœจ RE1 โœจfind/replace regexpRE1( \<regular expression with parentheses> )
โœจ RER โœจfind + custom replaceRER( 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_PID can extract the PID from the output of the commonly used ps aux command (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 cut command: 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 python in 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 node installed on the system. ๐Ÿ‘ If you don't want to install node, an alternative is to use docker.

๐Ÿ“Œ 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 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 โŒจ๏ธ

docs/internals.md

๐Ÿ“š Tutorial

(comming soon)

0.5.0

2 years ago

0.6.0

2 years ago

0.4.2

2 years ago

0.4.1

2 years ago

0.3.10

2 years ago

0.3.9

2 years ago

0.3.8

2 years ago

0.3.7

2 years ago

0.3.6

2 years ago

0.3.5

2 years ago

0.3.4

2 years ago

0.3.3

2 years ago

0.3.2

2 years ago

0.0.3

2 years ago