├── .gitignore ├── .jshintrc ├── package.json ├── index.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .idea 3 | *.log 4 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node": true, 3 | "bitwise": true, 4 | "browser": true, 5 | "camelcase": true, 6 | "curly": true, 7 | "eqeqeq": true, 8 | "esnext": true, 9 | "immed": true, 10 | "indent": 4, 11 | "latedef": true, 12 | "newcap": true, 13 | "noarg": true, 14 | "quotmark": "single", 15 | "regexp": true, 16 | "smarttabs": true, 17 | "strict": true, 18 | "sub": true, 19 | "trailing": true, 20 | "undef": true, 21 | "unused": true, 22 | "jquery": true, 23 | "es3": true, 24 | "globalstrict": true, 25 | "laxcomma": true, 26 | "predef": { 27 | "__dirname": true 28 | } 29 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-shell-parser", 3 | "version": "0.1.0", 4 | "description": "Tiny and handy lib for parsing the usual space-separated tables we get as shell commands outputs", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/namshi/node-shell-parser.git" 12 | }, 13 | "keywords": [ 14 | "shell", 15 | "table", 16 | "parse", 17 | "output" 18 | ], 19 | "author": "hossam-fares ", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/namshi/node-shell-parser/issues" 23 | }, 24 | "homepage": "https://github.com/namshi/node-shell-parser", 25 | "dependencies": { 26 | "lodash": "^2.4.1" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function parse(output, options) { 4 | options = options || {}; 5 | var separator = options.separator || ' '; 6 | var lines = output.split('\n'); 7 | 8 | if (options.skipLines > 0) { 9 | lines.splice(0, options.skipLines); 10 | } 11 | 12 | var headers = lines.shift(); 13 | var splitHeader = headers.split(separator); 14 | 15 | var limits = []; 16 | 17 | for (var i = 0; i < splitHeader.length; i++) { 18 | var colName = splitHeader[i].trim(); 19 | 20 | if(colName !== '') { 21 | limits.push({label: colName, start: headers.indexOf(colName)}); 22 | } 23 | } 24 | 25 | var table = lines.map(function(line) { 26 | if(line){ 27 | var result = {}; 28 | 29 | for (var key in limits) { 30 | var header = limits[key]; 31 | var nextKey = parseInt(key, 10)+1; 32 | var start = (key === '0') ? 0 : header.start; 33 | var end = (limits[nextKey]) ? limits[nextKey].start - start : undefined; 34 | 35 | result[header.label] = line.substr(start, end).trim(); 36 | } 37 | 38 | return result; 39 | } 40 | }); 41 | 42 | (table[table.length-1] === undefined) && table.pop(); 43 | 44 | return table; 45 | }; 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # From shell output to json 2 | 3 | Converts the usual, space separated, table from a 4 | shell command into a list of json object where the 5 | keys are the columns' names and the values the 6 | different data for each row. 7 | 8 | ## Install 9 | 10 | You can install this library through [NPM](https://www.npmjs.org/package/node-shell-parser): 11 | 12 | ```bash 13 | npm install node-shell-parser 14 | ``` 15 | 16 | ## Definition: 17 | 18 | ```javascript 19 | shellParser(shellOutput, options); 20 | ``` 21 | 22 | * `shellOutput`: the string resulting from running your command 23 | * `options.separator`: which character separates your tabled data, default is one space 24 | * `options.skipLines`: how many lines to skip before meeting the columns definition header 25 | 26 | ## Usage 27 | 28 | Execute a process, get its output and then simply 29 | feed it to the parser: 30 | 31 | ``` javascript 32 | 33 | var shellParser = require('node-shell-parser'); 34 | var child = require('child_process'); 35 | 36 | var process = child.spawn('ps'); 37 | var shellOutput = ''; 38 | 39 | process.stdout.on('data', function (chunk) { 40 | shellOutput += chunk; 41 | }); 42 | 43 | process.stdout.on('end', function () { 44 | console.log(shellParser(shellOutput)) 45 | }); 46 | ``` 47 | 48 | Black magic in action: 49 | 50 | ``` 51 | ~/projects/namshi/node-shell-parser (master ✘)✭ ᐅ node test.js 52 | [ { PID: '729', TTY: 'pts/1 00:0', TIME: '0:00', CMD: 'zsh' }, 53 | { PID: '057', TTY: 'pts/1 00:0', TIME: '0:00', CMD: 'node' }, 54 | { PID: '059', TTY: 'pts/1 00:0', TIME: '0:00', CMD: 'ps' } ] 55 | 56 | ``` 57 | --------------------------------------------------------------------------------