Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,16 @@ sed(/.*DELETE_THIS_LINE.*\n/, '', 'source.js');
Reads an input string from `file` and performs a JavaScript `replace()` on the input
using the given search regex and replacement string. Returns the new string after replacement.

#### grep(regex_filter, file [, file ...])
#### grep(regex_filter, file_array)
#### grep([options ,] regex_filter, file [, file ...])
#### grep([options ,] regex_filter, file_array)
Available options:

+ `-v`: Inverse the sense of the regex and print the lines not matching the criteria.

Examples:

```javascript
grep('-v', 'GLOBAL_VARIABLE', '*.js');
grep('GLOBAL_VARIABLE', '*.js');
```

Expand Down Expand Up @@ -368,3 +372,4 @@ Returns true if all the given paths exist.
_This function is being deprecated. Use `silent(false) instead.`_

Enables all output (default)

15 changes: 12 additions & 3 deletions shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,18 +659,26 @@ function _sed(options, regex, replacement, file) {
exports.sed = wrap('sed', _sed);

//@
//@ #### grep(regex_filter, file [, file ...])
//@ #### grep(regex_filter, file_array)
//@ #### grep([options ,] regex_filter, file [, file ...])
//@ #### grep([options ,] regex_filter, file_array)
//@ Available options:
//@
//@ + `-v`: Inverse the sense of the regex and print the lines not matching the criteria.
//@
//@ Examples:
//@
//@ ```javascript
//@ grep('-v', 'GLOBAL_VARIABLE', '*.js');
//@ grep('GLOBAL_VARIABLE', '*.js');
//@ ```
//@
//@ Reads input string from given files and returns a string containing all lines of the
//@ file that match the given `regex_filter`. Wildcard `*` accepted.
function _grep(options, regex, files) {
options = parseOptions(options, {
'v': 'inverse'
});

if (!files)
error('no paths given');

Expand All @@ -690,7 +698,8 @@ function _grep(options, regex, files) {
var contents = fs.readFileSync(file, 'utf8'),
lines = contents.split(/\r*\n/);
lines.forEach(function(line) {
if (line.match(regex))
var matched = line.match(regex);
if ((options.inverse && !matched) || (!options.inverse && matched))
grep += line + '\n';
});
});
Expand Down
4 changes: 4 additions & 0 deletions test/grep.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ var result = shell.grep('line', 'resources/a.txt');
assert.equal(shell.error(), null);
assert.equal(result.split('\n').length - 1, 4);

var result = shell.grep('-v', 'line', 'resources/a.txt');
assert.equal(shell.error(), null);
assert.equal(result.split('\n').length - 1, 8);

var result = shell.grep('line one', 'resources/a.txt');
assert.equal(shell.error(), null);
assert.equal(result, 'This is line one\n');
Expand Down