Giter Site home page Giter Site logo

find-in-files's Introduction

find-in-files Build Status Coverage Status

A simple tool to search text patterns across multiple files

Installation

find-in-files is a node module available via npm. You can install it with

$ npm install --save find-in-files

Usage

The module exposes two simple functions which expect three parameters each.

// Async
find(pattern, directory, fileFilter)
// Sync
findSync(pattern, directory, fileFilter)

pattern [string|object]

The string you want to search for or object to control regex flags

directory [string]

The directory you want to search in.

fileFilter [regex] (optional)

A regex you can pass in to only search in files matching the filter.

var findInFiles = require('find-in-files');

Both functions return a promise which will receive the results object. The results object contains the matches, count of matches per file and the lines that match.

{
    'fileOne.txt': {
        matches: ['found string'],
        count: 1,
        lines: ['This line contains a found string.']
    }
}

Example

findInFiles.find("I'm Brian, and so's my wife!", '.', '.txt$')
    .then(function(results) {
        for (var result in results) {
            var res = results[result];
            console.log(
                'found "' + res.matches[0] + '" ' + res.count
                + ' times in "' + result + '"'
            );
        }
    });
// Use object to set flags on regular expression. This one will ignore case.
findInFiles.find({'term': "I'm Brian, and so's my wife!", 'flags': 'ig'}, '.', '.txt$')
    .then(function(results) {
        for (var result in results) {
            var res = results[result];
            console.log(
                'found "' + res.matches[0] + '" ' + res.count
                + ' times in "' + result + '"'
            );
        }
    });

License

MIT © Philipp Nowinski

find-in-files's People

Contributors

creeation avatar elgolfin avatar kaesetoast avatar mistergf avatar peternoordijk avatar thunderrun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

find-in-files's Issues

Memory issue

Thanks a lot for the amazing package!

I am trying to use it to search logs files throw my project.

I am getting this error in the Node console

<--- Last few GCs --->

[17904:0000014A7121EE00] 13909 ms: Scavenge 1960.9 (2056.5) -> 1959.1 (2071.5) MB, 23.8 / 0.0 ms (average mu = 0.866, current mu = 0.628) allocation failure
[17904:0000014A7121EE00] 14304 ms: Mark-sweep 1974.0 (2071.5) -> 1972.1 (2087.5) MB, 374.3 / 0.0 ms (average mu = 0.726, current mu = 0.367) allocation failure scavenge might not succeed

<--- JS stacktrace --->

FATAL ERROR: MarkCompactCollector: young object promotion failed Allocation failed - JavaScript heap out of memory
1: 00007FF793E1058F napi_wrap+109311

I understand it is a memory issue..
there is any workaround for it?

currently there are 10 folders in the root. and each of them contain 2 more logs file.
Each of the logs files contain 5 line of text

Thanks a lot!

Is it possible to find in file using full path?

I'm trying to write a script where you pass a full path to a file and it will search it but when i run the search I get

{ Error: ENOENT: no such file or directory, lstat '/Users/username/Development/internal/test.html '
  errno: -2,
  code: 'ENOENT',
  syscall: 'lstat',
  path: '/Users/username/Development/internal/test.html ' }

Can I find a file using the full path or does it need to be in the same directory as the script?

Use glob?

It'd be nice to have glob support for file matching. Because when I set a folder which contains a node_modules... 😅

Error-prone use of an array as a key-value object

Look at the function getResults starting on line 69 and how the results array is used as a key-value data structure:

function getResults(content) {
    var results = []

    for (var i = 0; i < content.length; i++) {
        var fileMatch = content[i].value;
        if (fileMatch.match !== null) {
            results[fileMatch.filename] = {
                matches: fileMatch.match,
                count: fileMatch.match.length,
                line: fileMatch.lines
            };
        }
    }

    return results;
}

Although being valid JavaScript, this seems quite unconventional and leads to weird behaviour. For example, results.length equals zero. Is this a programming error? If the author intended to preserve the order, this is not the way how to do it. Arrays are objects and the order of the keys of an object is unspecified.

I guess there are two solutions. The first is simply var results = {}. The second option would be to still use an array but instead push objects into it, with additional key filename:

results.push({
  filename: fileMatch.filename,
  matches: fileMatch.match,
  count: fileMatch.match.length,
  line: fileMatch.lines
});

Can we search for multiple strings ina file ?

Hello,

I want to search multiple string values in a file using your library. Is that possible ?


findInFiles.find({'term': "str1 | str2 | str3" , 'flags': 'ig'}, '.', '.txt$')
    .then(function(results) {
        for (var result in results) {
            var res = results[result];
            console.log(
                'found "' + res.matches[0] + '" ' + res.count
                + ' times in "' + result + '"'
            );
        }
    });

Error thrown when using simple regex like

error is thrown when I ignore regex, or put simple regex like '.js$'. Why it doesn't work?

findInFiles.find("xxx", 'xxxx', '.js$')
    .then(function(results) {
        for (var result in results) {
            var res = results[result];
            console.log(
                'found "' + res.matches[0] + '" ' + res.count
                + ' times in "' + result + '"'
            );
        }
    });
`/Users/msu/test/node_modules/q/q.js:155
                throw e;
                ^

TypeError: Cannot read property 'match' of undefined
    at getResults (/Users/msu/test/node_modules/find-in-files/index.js:74:22)
    at /Users/msu/test/node_modules/find-in-files/index.js:92:30`

Find a result that involve more lines in the file

Hi all, I kindly ask for a support. I need to search in mid-size text files (one at the time, 50/60 MB size) for regexp that search for 2 strings one "near" the other.
The use case is to automatically search for patterns (request/answer) in log files.

Example:
text file fragment

000000: USB-SERIAL CH340 (COM12), 2018-11-26 17:35:54,3748068
 0A 03 00 00 00 17 04 BF                           .......¿
000001: USB-SERIAL CH340 (COM10), 2018-11-26 17:35:54,9676068 +0,5928000
 0A 03 2E 00 01 00 00 00 18 00 18 00 19 00 18 00   ................
 19 00 31 00 01 00 01 00 01 00 01 01 CC 70 DE C3   ..1.........ÌpÞÃ
 4F 95 02 00 00 00 00 00 00 00 00 00 00 00 00 0C   O•..............
 B7 E5 45                                          ·åE

First i search for the request pattern using theexample code with this "regexp" and i find the expected result:

findInFiles.find({'term': "0A 03 00 00 00 17 04 BF", 'flags': 'gm'}, '.', '.txt$')
found "0A 03 00 00 00 17 04 BF" 752 times in "181127_0944_ReqView.txt"

Then i search for the req/ans pattern where the result involve search in multiple lines and it finds nothing:

findInFiles.find({'term': "0A 03 00 00 00 17 04 BF\W+(?:\w+\W+){1,40}? 0A 03 2E", 'flags': 'gm'}, '.', '.txt$')

Using the same regexp in notepad++ search returns the expected results:
image

Can someone kindly give me an hint (i'm not an expert programmer i'm writing just few lines of code to automate field tests) ?
Thanks a lot

Possible to move on after first hit?

Hi, thank you for this great package.

Would it be possible to end a search as soon as the string is found once? I am comparing a list of strings to a list of files, and I have no need to know if the string is found more than the first hit.

Thank you

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.