清理sql文件中INSERT INTO 内容

清理sql文件中INSERT INTO 内容

const fs = require('fs');
const path = require('path');

// 读取 SQL 文件
const sqlFilePath = path.join(__dirname, 'index.sql');
let sqlContent = fs.readFileSync(sqlFilePath, 'utf-8');

// 将文件内容按行分割
const lines = sqlContent.split('\n');

// 用于存储清理后的内容
let cleanedContent = '';

// 记录删除的行数和总行数
let deletedLines = 0;
let totalLines = lines.length;

// 遍历每一行
lines.forEach((line, index) => {
  // 检查是否包含 INSERT INTO 语句
  if (line.includes('INSERT INTO')) {
    // 输出删除的行内容和工作进度
    console.log(`[Deleted] Line ${index + 1}: ${line.trim()}`);
    console.log(`Progress: ${((index + 1) / totalLines * 100).toFixed(2)}%`);
    deletedLines++;
  } else {
    // 如果不包含 INSERT INTO,则保留该行
    cleanedContent += line + '\n';
  }
});

// 输出统计信息
console.log(`\nTotal lines: ${totalLines}`);
console.log(`Deleted lines: ${deletedLines}`);
console.log(`Remaining lines: ${totalLines - deletedLines}`);

// 输出清理后的内容(或写回文件)
console.log('\nCleaned content:\n');
console.log(cleanedContent); // 输出到控制台
fs.writeFileSync(sqlFilePath, cleanedContent, 'utf-8'); // 写回文件

 

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容