one-liners node.js, helps to write one line stdin filter program by node.js Javascript like perl/ruby. + JSON/CSV/Promise/Async/MultiStream feature(CLI tool/module)
$ cat test.txt
Hello World
Goodnight World
$ cat test.txt | norl -pe '$_=$_.replace(/World/,"Norl")'
Hello Norl
Goodnight Norl
# -p: execute -e <program> line by line. $_: input/output line from/to stdin/out
$ cat test2.txt
Apple,12
Google,3
$ cat test2.txt | norl -B 'total=0' -ane 'total+=Number($F[1])' -PE '$_=`total:${total}`'
total:15
# -n: same as p but doesn't print at -e <program> line by line
# -a: $F=$_.split(',') before -e <program>
# -B <program> / -E <program>: execute <program> before(-B) / after(-E) stdin processing of -e <program>
# -P print $_ at end of stream
npm install -g norl
$ cat README.md | wc -l | sed 's/^ *//'
#wc -l prints unnecessary white space
$ cat README.md | norl -aPe '=>$F.length'
#norl version
$ cat test.json
{"a":1,"b":2}
$ cat test.json|norl -jJ
{
"a": 1,
"b": 2
}
$ norl -Pe '$_=$_.replace(/_VERSION_/g,"1.2.0")' -O destDir/ package.json README.md LICENSE.txt
destDir/package.json
destDir/README.json
destDir/LICENSE.json (All files "_VERSION_" strings were replaced by 1.2.0)
$ cat address.csv
norl,moon
partpipe,mars
$ cat tel.csv
norl,010-342-234
partpipe,010-122-444
$ norl address.csv tell.csv a.csv tel.csv -B 'res={};' -ane '_.set(res,[$F[0],$S],$F[1]);' -E '_.forIn(res,(v,r)=>{$P([r,v[0],v[1]].join(","))})'
norl,010-342-234,moon
partpipe,010-122-444,mars
$ npm i -g twitter-tool; twitter -i
$ cat list.csv
foo,12345
bar,13432
$ TWITTER_USER=kssfilo LST=list2 CSVFLD=0 norl -anxe $'=>`twitter lists/members/create -o s:${$e("LST")},s_n:${$F[$e("CSVFLD")]},o_s_n:${$e("TWITTER_USER")} `' list.csv
# user @foo and @bar has been added to list2 of @kssfilo
@PARTPIPE@|dist/cli.js -h| perl -pe 'if(!m/^ /){s//\/g}'
You can see detail usage on npmjs.com or norl -h
@PARTPIPE@
norl provides module interface. you can write your own CLI filter program easier.
npm install norl
echo '{"s":"Hello World"}'| node -e 'require("norl").e(($G,$_)=>{console.log(JSON.parse($_).s);})'
# Hello World
echo -e "Hello\nWorld"| node -e 'require("norl").ne(($G,$_)=>{console.log($_.replace(/Hello/,"Hi!"))})'
# Hi!
# World
echo -e "Hello\nWorld"| node -e 'require("norl").ne(($G,$_)=>{$G.count+=$_.length},($G)=>{$G.count=0},($G)=>{console.log(`Chars:${$G.count}`)})'
# Chars:10
# require.("norl").r(<function(-e)>)
# require.("norl").e(<function(-e)>)
# require.("norl").e(<RegExp|String>,<function(-e)>)
# require.("norl").ne(<function(-e)>,[<function(-B)>,<Function(-E)>)
# require.("norl").ne(<RegExp|String>,<function(-e)>,[<function(-B)>,<Function(-E)>])
# <RegExp|String> is seperator for .split()
# function(-e): function($G,$_,$F){...}
# function(-B): function($G){...}
# function(-E): function($G){...}
# $G is global object for communicating each functions.
- 3.1.x: adds $e(name) (shorthand for process.env[name]) / able to use => with -xpe -Xpe like =>'echo hello'
- 3.0.x: adds $s /
$m shorthands for $ .replace()/ $.match() - 3.0.x: -pe 'return "FOO"' prints "FOO" instead of $_. adds special abbriviation '=>...'
- 3.0.x: Breaking change: returning Number will print out the number instead of return code. if you return code like test command, use Boolean.
- 2.4.x: -m './foo/bar' style module path support/-S search node_modules before NODE_PATH/async func at -B suppport/automatic => detection/automatic print for async func on -pe
- 2.3.x: added Multi-Input-Multi-Out mode (-O)
- 2.2.x: supports file input and multi-input mode.
- 2.1.x: controling process.exit(n) code by returning number at final function.(-P -J will be cancelled)
- 2.0.x: -x/-X option, async.js style callback support. -L option
- 1.1.x: adds -c option/able to omit -a when -F is specified
- 1.0.x: first release