Поскольку вы работаете с файлами JSON, почему бы не разобрать его как таковой? Установите nodejs-legacy и создайте сценарий NodeJS, например:
#!/usr/bin/env node
// parseline.js process lines one by one
'use strict';
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
rl.on('line', function(line){
var obj = JSON.parse(line);
// add the fields which you want to extract here:
var fields = [
obj.data.headers.to,
obj.data.headers.subject,
// etc.
];
// print the fields, joined by a comma (CSV, duh.)
// No escaping is done, so if the subject contains ',',
// then you need additional post-processing.
console.log(fields.join(','));
});
Предполагая, что у вас есть строка JSON в каждой строке файла:
node parseline.js < some.txt
Или, если вы действительно хотите прочитать один файл и проанализировать поля:
#!/usr/bin/env node
// parsefile.js - fully read file and parse some data out of it
'use strict';
var filename = process.argv[1]; // first argument
var fs = require('fs');
var text = fs.readFileSync(filename).toString();
var obj = JSON.parse(text);
// add the fields which you want to extract here:
var fields = [
obj.data.headers.to,
obj.data.headers.subject,
// etc.
];
// print the fields, joined by a comma (CSV, duh.)
// No escaping is done, so if the subject contains ',',
// then you need additional post-processing.
console.log(fields.join(','));
Затем запустите его с помощью:
node parsefile.js yourfile.json > yourfile.csv