Created
April 21, 2021 15:44
-
-
Save GaetanoPiazzolla/b0b3278da7130c80eec2c218ff5321bb to your computer and use it in GitHub Desktop.
Search string in file NodeJS - FULL file in memory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const osType = require('os').type(); | |
| const {spawn} = require('child_process'); | |
| const searchOs = (filename, text) => { | |
| return new Promise((resolve) => { | |
| if (osType === 'Windows_NT') { | |
| console.log('Searching using WINDOWS FIDSTR'); | |
| let child = spawn('findstr', [ | |
| '/i', | |
| '/s', | |
| '/c:' + text, 'file/' + filename + '.txt'], | |
| {cwd: 'C:/workspace_private/nodejs-search-text/'}); | |
| let results = [] | |
| child.stdout.on('data', function (data) { | |
| if (data) { | |
| results = results.concat(data.toString().split("\r\n")) | |
| results.pop() | |
| } | |
| }); | |
| child.stderr.on('data', function (data) { | |
| console.log('stderr: ' + data); | |
| }); | |
| child.on('close', function (code) { | |
| console.log('child process exited with code ' + code); | |
| resolve(results) | |
| }); | |
| } | |
| }) | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Related to article https://medium.com/codex/achieve-the-best-performance-search-a-string-in-file-using-nodejs-6a0f2a3b6cfa