const fs = require('fs'); const absPath = `D:/sql_export`; // 数字转成字符串,同时在前面填充 function fill(num, fillers, length) { var result = `${num}`; if (result.length < length) result = new Array(length - result.length + 1).join(fillers) + result; return result; } let begin = 115; let end = 116; for (let i = begin; i < end; i++) { console.log(`读取文件 comment_export_${fill(i, '0', 4)}.sql`); let sqlFile = fs.readFileSync(`${absPath}/comment_export_${fill(i, '0', 4)}.sql`, "utf-8"); console.log(`处理文件`); // console.log(sqlFile); let sqls = sqlFile.split("\r\n"); sqls = sqls.map(sql => sql.replace(`INSERT INTO \`comment_export_${fill(i, '0', 4)}\` VALUES`, "INSERT INTO `comment` VALUES")) // console.log(sqls); console.log(`拼接文件`); let newSqlFile = sqls.join('\n'); console.log(`写入文件`); fs.writeFileSync(`${absPath}/output/comment_${fill(i, '0', 4)}.sql`, newSqlFile, "utf-8"); console.log(`完成`); }