I'm thinking whether it is possible to pass an array as input of awk
command via Node.JS child_process.spawn()
.
I'll explain myself with an example.
Let's define an array (in my real case the array has about 3 million and something rows) :
const myvar = ['val1', 'val2', 'val1', 'val3', 'another', 'etc', 'etc'];
Now, using child_process
, I want to pass this variable as input of awk
command, remove duplicates and save the output into a text file.
Wrongly, I'm currently using a template string, thinking that maybe the command would read from the variable:
const { spawn } = require('child_process');
const awk = spawn(
`awk '!a[$0]++' ${myvar} > outputfile.txt`
);
but it's obviously a wrong usage. Since, I get:
- awk exits with code 2 (wrong usage)
or also:
- I get
Error: spawn E2BIG
I'm finding myself stuck in this issue. I know I can pass a simple string variable but it's different.
.
Is something like this doable?
N.B.: the array is taken from a text file loaded into the code at an earlier stage. That's why I don't want to read from the file again. Using the text file as input works okay with awk
but I'm trying to avoid reading that multiple times.
If I didn't explain clearly, please let me know. Thanks for help!