1. const fs = require('fs');
  2. const path = require('path');
  3. /**
  4. * Strips metadata from a log file.
  5. * @param {string} inputFilePath Path to the input log file.
  6. * @param {string} outputFilePath Path to the output log file (without metadata).
  7. */
  8. function stripMetadata(inputFilePath, outputFilePath) {
  9. fs.readFile(inputFilePath, 'utf8', (err, data) => {
  10. if (err) {
  11. console.error(`Error reading file: ${err}`);
  12. return;
  13. }
  14. // Simple metadata removal: removes lines starting with common metadata indicators.
  15. const metadataIndicators = ['#', 'Date:', 'Time:', 'Source:'];
  16. const lines = data.split('\n');
  17. const cleanedLines = lines.filter(line => !metadataIndicators.some(indicator => line.startsWith(indicator)));
  18. const cleanedData = cleanedLines.join('\n');
  19. fs.writeFile(outputFilePath, cleanedData, 'utf8', (err) => {
  20. if (err) {
  21. console.error(`Error writing to file: ${err}`);
  22. return;
  23. }
  24. console.log(`Metadata stripped from ${inputFilePath} and saved to ${outputFilePath}`);
  25. });
  26. });
  27. }
  28. //Example usage:
  29. // stripMetadata('input.log', 'output.log');

Add your comment