parse-columns
Parse text columns, like the output of unix commands
Last updated 9 years ago by sindresorhus .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install parse-columns 
SYNC missed versions from official npm registry.

parse-columns

Parse text columns, like the output of Unix commands

Install

npm install parse-columns

Usage

$ df -kP
Filesystem 1024-blocks      Used Available Capacity  Mounted on
/dev/disk1   487350400 467871060  19223340    97%    /
devfs              185       185         0   100%    /dev
map -hosts           0         0         0   100%    /net
import {promisify} from 'node:util';
import childProcess from 'node:child_process';
import parseColumns from 'parse-columns';

const execFileP = promisify(childProcess.execFile);

const {stdout} = await execFileP('df', ['-kP']);

console.log(parseColumns(stdout, {
	transform: (item, header, columnIndex) => {
		// Coerce elements in column index 1 to 3 to a number
		if (columnIndex >= 1 && columnIndex <= 3) {
			return Number(item);
		}

		return item;
	}
}));
/*
[
	{
		Filesystem: '/dev/disk1',
		'1024-blocks': 487350400,
		Used: 467528020,
		Available: 19566380,
		Capacity: '96%',
		'Mounted on': '/'
	},
	…
]
*/

API

parseColumns(textColumns, options?)

textColumns

Type: string

The text columns to parse.

options

Type: object

separator

Type: string Default: ' '

Separator to split columns on.

headers

Type: string[]

Headers to use instead of the existing ones.

transform

Type: Function

Transform elements.

Useful for being able to cleanup or change the type of elements.

The supplied function gets the following arguments and is expected to return the element:

  • element (string)
  • header (string)
  • columnIndex (number)
  • rowIndex (number)

Related

Current Tags

  • 3.0.0                                ...           latest (3 years ago)

7 Versions

  • 3.0.0                                ...           3 years ago
  • 2.0.0                                ...           5 years ago
  • 1.3.0                                ...           9 years ago
  • 1.2.0                                ...           9 years ago
  • 1.1.0                                ...           9 years ago
  • 1.0.1                                ...           9 years ago
  • 1.0.0                                ...           9 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (4)
Dev Dependencies (1)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |