I have a set of template files present in folder say /root/path/templates/
and corresponding input files at path say /root/path/inputs/<template_name>/
. Here <template_name>
indicates the folder name which holds all the input files for a given template.
Let's say template file
contains v
- List item
alue as %1 %2
which are like place holders, where we need to replace the %n
with the line number from input file line which are present in /root/path/inputs/<template_name>/
.
For example:
template file name as `t1.template` and its content are `%1 %2`
Input files are `in1.input` and `in2.input` at path `/root/path/inputs/t1`.
The content for in1.input as:
First
Second
The content for in2.input is
Third
Fourth
My expected output should be like this:
t1/in1.output
First Second
t1/in2.output
Third Fourth
Now the template file can have any characters say:
`This is a template file with %1 %2 fields. Also it has %%1 percenage`
Here the corresponding output should be :
t1/in1.output
This is a template file with First Second fields. Also it has %1 percenage
t1/in2.output
This is a template file with Third Fourth fields. Also it has %1 percenage
means only %1 and %2 placeholders should be replaced. If %%n then while generating result show it as %n, it is not treated as placeholder.
Now I am trying to implement this using JS and Node:
var fs = require('fs');
var arr = fs.readdirSync("/root/path/templates");
var response = "";
for(let i=0; i<arr.length; i++) {
var item = arr[i];
var hastemplate = item.indexOf(".template");
if(hastemplate > -1) {
var tempName = item.substring(0, hastemplate);
var lineReader = require("readline").createInterface({
input: fs.createReadStream("/root/path/inputs/"+tempName)
});
lineReader.on("line", function(line) {
console.log(line + ' ' +tempName);
});
}
}
Input to this program:
/root/path/templates/t1.template
/root/path/inputs/t1/in1.input
/root/path/inputs/t1/in2.input
/root/path/templates/t2.template
When I try to print the file name in lineReader
I am getting it as t2.template
, i am not able to properly read the data from t1.template and its files in1.input and in2.input.
I also want to know how to read the input files and maintain the order for the output.
So got stuck here with my code incomplete.