Grep
This is a simple wrapper around grep(1)
. The module use child_process
to create
grep
processes and use streams and callbacks to communicate.
Install
Using npm:
$ npm install grep1
From source:
$ git clone https://github.com/ladinu/node-grep.git$ cd node-grep$ npm link
Examples
Grep file /tmp/file
for expression spurs:
var grep = ; ;
Grep file /tmp/file
for expression spurs:
var search = ; search; search;
Using request, grep example.com for expression examples
var req = ;var search = ; search;;
API
Class: Grep
This class wrap grep(1)
using node's child_process
module. When a callback is given
child_process.execFile()
is used and returns a Grep
object which is a destroyed stream.
When no callback, child_process.spawn()
is used and returns a Grep
object which is a
stream.
Event: 'error'
Process's stderr
is emitted as error
events.
Event: 'data'
Process's stdout
is emitted as data
events.
grep([args], [options], callback)
-
args
The arguments given togrep(1)
processes. By defaultargs
is concatinated with an empty array and passed togrep(1)
process. Seeoptions
to change this behaviour. -
options
Object forGrep
object. Note-
buildArgs
Function that build arguments forgrep(1)
processes. By default this is set to a function that concatinate givenargs
to an empty array. So when you callgrep("some_phrase")
the actual arguments given togrep(1)
process is[ 'some_phrase' ]
. -
execOptions
Object that give options such ascwd
to eachgrep(1)
process. By default this is an empty object. This is the sameoptions
object that you would use when callingchild_process.execFile()
orchild_process.spawn()
. Because a callback is given,child_process.execFile()
is used. See node documentation for more information.
-
-
callback
Function that is called whengrep(1)
process terminateerror
Errorstdout
Bufferstderr
Buffer
-
Returns a
Grep
object
grep([args], [options])
-
args
Same asgrep([args], [options], callback)
-
options
Same asgrep([args], [options], callback)
exceptchild_process.spawn()
is used. See node documentation for more infromation -
Returns a
Grep
object
grep.configure([options])
This will make options
global when options
is not given to grep([args], [options])
or grep([args], [options], callback)
- Returns: nothing
For example:
var grep = ; // Set buildArgs so grep(1) will count line numbers.// Set current working directory to '/var/log' and perform// grep operations on file 'some.log'//// When you call grep("phrase"), a new grep(1) process will// be created with arguments ['-n', 'phrase', 'some.log'] var fs = ;warningFile = fs;errorFile = fs; grep; var warning = ;var errors = ; warning;errors; // Grep file '/tmp/foo.txt' for first occurance of 'bar'// Note: global options set above do not apply because we // are giving an an empty options object var options = {};var bar = ;bar;
grep.resetConfig()
Helper method to clear global settings. This basically set buildArgs
to null
and
execOptions
to an empty object.
License
Copyright (c) 2013 Ladinu Chandrasinghe
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.