Reputation: 1133
I'm trying to recursively grep for ip addresses in a directory.
I have two files, one
and two
:
└┼─$─┤▶ cat one
test
192.168.1.2
192.168.1.1102
182982
19829872.28288222.222982
sqdqssdsqd:12.92822.sldql
192.168.1.91
└┼─$─┤▶ cat two
edezdzedezdezdezd:&&122.12
&&é&
ddzez
efreffe
np.ip
Here's how I use grep to get ip's:
└┼─$─┤▶ grep -sRIEoh '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
192.168.1.2
192.168.1.110
192.168.1.91
192.99.99.99
And I'm tring to do the same in node using child_process:
var spawn = require('child_process').spawn
var child
function puts(error, stdout, stderr) { console.log(stdout,error,stderr); process.exit(); }
const grep =spawn('grep',['-sRIEoh' ,'[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}','/home/user/test'])
const uniq = spawn('uniq');
grep.stdout.pipe(uniq.stdin);
grep.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
But the result is different for some reason...
└┼─$─┤▶ node test.js
stdout: 192.168.1.2
192.168.1.110
19829872.282
88222.222982
12.92822
192.168.1.91
192.99.99.99
Upvotes: 0
Views: 81
Reputation: 47099
Backslashes is used as a escape character in JavaScript, almost like it's in double quotes in Bash, the main exception is that JavaScript will remove the backslash if it cannot be used in a escape, consider the following:
% echo "\y"
\y
% node -e "console.log('\y')"
y
This means that you will need to escape backslashes that are used to escape .
in grep:
[0-9]{1,3}\.
should be:
[0-9]{1,3}\\.
The full spawn line:
const grep = spawn('grep', ['-sRIEoh',
'[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}','/home/user/test']);
Upvotes: 1