• Stars
    star
    291
  • Rank 142,563 (Top 3 %)
  • Language
    JavaScript
  • Created over 11 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Node Input/output Piper

nip - Node Input/output Piper

NPM version Build status Test coverage Downloads

nip is a command line utility for performing any type of processing to and from files and pipes

Install

npm install nip -g

If you omit the -g then make sure to add the local npm module path to your default path

You should now be able to call nip from the command line.

Usage: nip js-function [options] [files]

The js-function can be one of three syntaxes:

  1. return line.substr(0, 10) + index
  2. function(line, index, cols, lines) { /* code here */ return value; }
  3. /* code */ return function(line, i, cols, lines) { /* ... */ return value; }

The names line, index, lines, and cols can be changed in the second and third style syntaxes

If the return value is false nothing is sent to output stream
If the return value is not a string or number then the line will be sent to the output stream
else the return value will be sent to the output stream (including an empty string)

options

-f js-file or --file=js-file

use the js-file as the function to execute on the input instead of the js-function argument you must supply either this option or the js-function argument


-1 or --first-line-only

only execute once per file, not for each line
this is useful if you plan on proccessing the file as a whole, namely through the lines variable
for example (not a useful one): nip 'return lines.length' -1 file.txt


-c string-or-regex, --col-splitter=string-or-regex

the splitter for --cols, can be regex or string format, by default it's /\s+/


-s string-or-regex, --line-splitter=string-or-regex

the line separator, can be regex or string format, by default we're splitting on lines so it's \n


-n string, --line-joiner=string

string used to join lines together


Examples

Only output lines that begin with the word var:

nip 'function(l) { return /^var/.test(l); }' lines-that-start-with-var.txt

Output every second line only in uppercase in a file:

nip 'function(line, i) { return i % 2 ? line.toUpperCase() : false; }' every-2nd-line.txt

Trim whitesplace from a file:

nip 'return line.replace(/^s*|s*$/g, "");' trim-lines.txt

Run the contents of jsfile.js on file.txt:

nip -f jsfile.js file.txt

Like most unix commands, you can pipe the input and/or output:

generate a script file to rename files recursively and sequentiality
find . -type f | nip 'return "mv " + line + " " + line.replace(/\/[^/]*$/, "") + "/" + index;' > rename-script

rename files recursively and sequentiality
find . -type f | nip 'return "mv " + line + " " + line.replace(/\/[^/]*$/, "") + "/" + index;' | sh

find the biggest number from all files in a directory:

nip '
  var biggest = 0;
  this.on("end", function() { print(biggest); });
  return function(_,i,lines) {
    biggest = Math.max(biggest,
      Math.max.apply(Math, lines.match(/(?:\s|^)[\d]+(?:\.\d*)?(?:\s|$)/g))
    )
  }' -1 *

By default there are start, end, fileStart, and fileEnd events you can register by doing this.on('end', function() {})

The context inside the main function can be used as a global store, and has a filename property


Why

This is for people who aren't "devops" and who can't crank out a fancy piped shell script using awk and sed. Also most programmers who have node installed can write a quick javascript one+ liner to do what the oldschoolers would make a shell script out of.

https://github.com/kolodny/nip

More Repositories

1

immutability-helper

mutate a copy of data without changing the original source
TypeScript
5,174
star
2

exercises

Some basic javascript coding challenges and interview questions
JavaScript
4,174
star
3

safetest

TypeScript
1,307
star
4

wavy

use ~ in require and import calls
JavaScript
379
star
5

webwork

Execute Web Workers without external files
JavaScript
229
star
6

redux-await

Manage async redux actions sanely
JavaScript
155
star
7

zerobox

JavaScript
148
star
8

jsan

handle circular references when stringifying and parsing
JavaScript
90
star
9

redux-create-reducer

Publishing createReducer from http://redux.js.org/docs/recipes/ReducingBoilerplate.html#generating-reducers
JavaScript
84
star
10

weak-key

Get a unique key for an object ( mainly for react's key={} )
JavaScript
76
star
11

ts-cookbook

A collection of delicious Typescript recipes
TypeScript
44
star
12

screenliner

node util for writing to regions on the terminal
JavaScript
41
star
13

deact

react inspired DOM element template engine
JavaScript
33
star
14

TinyRouter.php

Simple Router For PHP
PHP
26
star
15

w8

Give promises and thunks timeouts
JavaScript
25
star
16

run-every

run a command over and over
JavaScript
23
star
17

crlf

get and set line endings - Pull Requests welcome
JavaScript
23
star
18

react-mocha-starter

Scaffolding for a react project with mocha testing in node and phantom
JavaScript
18
star
19

nester

Get and set properties from deeply nested arrays
JavaScript
16
star
20

zerovalidate

A minimalist javascript form validator
JavaScript
12
star
21

redux-browserify

it can be done
JavaScript
12
star
22

redux-standard-action

A human-friendly standard for Redux action objects.
JavaScript
11
star
23

jq2

extract json data
JavaScript
10
star
24

wttt

When This Then That
JavaScript
10
star
25

zan

test object types (similar to React.PropTypes)
JavaScript
7
star
26

httpies

httpie with extra headers
JavaScript
7
star
27

poel

Create a pool of cluster workers.
JavaScript
6
star
28

react-compose-wrappers

TypeScript
6
star
29

typeaheadObj

Twitter Bootstrap Typeahead With Objects
JavaScript
6
star
30

git-tree-maker

JavaScript
6
star
31

is_googlebot.php

PHP
4
star
32

zerocomplete

JavaScript
4
star
33

tweet-editor

HTML
3
star
34

mithril-todo-msx

JavaScript
3
star
35

fizzbuzz

JavaScript
3
star
36

reglob

require a glob
JavaScript
3
star
37

will-they-sue

list the license of all your deps
JavaScript
3
star
38

ccolors

Cli Colors, a slightly improved version of npm's colors
JavaScript
3
star
39

member-berry

Memoize a function of n args with O(n) recall and no memory leaks.
JavaScript
3
star
40

Db.class.php

PHP
2
star
41

react-track-events

TypeScript
2
star
42

babel-plugin-hodor

JavaScript
2
star
43

zerolay

JavaScript
2
star
44

jquery.save

jQuery.save
2
star
45

xurl

A simple url parser
JavaScript
2
star
46

es6-module-boilerplate

boilerplate for es6 -> es5 module
JavaScript
2
star
47

logen

lodash like library for generators
JavaScript
1
star
48

talks

HTML
1
star
49

Timer.class.php

PHP
1
star
50

CacheSQL

CacheSQL class
PHP
1
star
51

co-phantom

JavaScript
1
star
52

tuplizer

Powerful typescript types to manipulate tuples
TypeScript
1
star
53

wrt

A simple react router
JavaScript
1
star
54

torah

Python
1
star
55

webcam-switcher

TypeScript
1
star
56

promise-utils

Useful utils to use native promises like bluebird
JavaScript
1
star
57

isIE10

javascript test if browser is IE10
JavaScript
1
star
58

reflux-create-reducer

Publishing createReducer from http://rackt.github.io/redux/docs/recipes/ReducingBoilerplate.html
JavaScript
1
star
59

m2

Lightweight Mocha Clone
JavaScript
1
star
60

node-webkit-stater

JavaScript
1
star
61

rxjs-props

Like Promise.props but for rxjs
JavaScript
1
star
62

octopress-test-case

Shell
1
star
63

toopl

Typescript tuple
JavaScript
1
star
64

github-tab-resizer

JavaScript
1
star
65

react-override

TypeScript
1
star
66

require-es6

Require es6 version of module if available as `main-es6` prop in package.json
JavaScript
1
star