Coder Social home page Coder Social logo

nip's Introduction

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

nip's People

Contributors

kolodny avatar antham avatar gwgundersen avatar eiriksm avatar

Stargazers

Andrew Johnson avatar  avatar  avatar  avatar Ryota Bannai avatar D. Bohdan avatar ERIC-ERKI MÜÜR avatar Mateusz Bagiński avatar awakenrz avatar Ron Martinez avatar Sora Morimoto avatar Sarit Das avatar Alexey Okhrimenko avatar Alex Wendland avatar Dee Cheung avatar Dheeraj Bhaskar avatar Cat  avatar Haruka Asakura avatar  avatar Brian Peiris avatar Luka Kakia avatar  avatar liujian zhang avatar Justin Webster avatar Amio Jin avatar BrunoLee avatar Ernst Salzmann avatar Joe McCarver avatar Valentin Vichnal avatar Ben Creasy avatar Martin Milan avatar Chris Cinelli avatar Zac Barton avatar Josh Duff avatar wildeyes avatar ayapi avatar Angus H. avatar Hyunjin CHA avatar Chanje Park avatar Seoh Char avatar FigoZhu avatar Erich Ocean avatar Alexander avatar Michael Anthony avatar Kai Li avatar Orkan Alat avatar Sunguk Lee avatar Mladen Krivaćević avatar Clint Ayres avatar Clarence Leung avatar Yosh avatar Cheng Lou avatar Adam Veary avatar Matej Drame avatar Slava avatar Mikael Karon avatar wangyue avatar Sascha avatar  avatar Michael Scott Hertzberg avatar Riccardo Bartoli avatar haoran avatar Imran Hayder  avatar Devin Clark avatar Grigorii Chudnov avatar Ed Gauci avatar Aster Haven avatar Carlos Cárcamo avatar Nuno Nogueira avatar Gosha Spark avatar Chris Hart avatar Soheil Rashidi avatar  avatar Etai Peretz avatar c4605 avatar Samuel Bohler avatar Kyle Kellogg avatar 无止休 avatar Rob Howell avatar april avatar Johan Satgé avatar 尹良灿 avatar Jack Chen avatar Desen Meng avatar Subrat avatar Pat Cavit avatar Guo Yu avatar Young avatar afc163 avatar Olim Saidov avatar Craig Dennis avatar Moritz Klack avatar Zbyszek Tenerowicz avatar Dan Tamas avatar Mictian avatar Mark Smith avatar Chris Gaudreau avatar Naoto Yokoyama avatar thadclay avatar Rhys Arkins avatar

Watchers

Milan avatar evandrix avatar yury avatar Richard Hess avatar Bob McNaughton avatar  avatar Michael Anthony avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.