Created
August 9, 2014 19:16
-
-
Save hdf/d0de1c0df3fecd19cf50 to your computer and use it in GitHub Desktop.
incremental regex replace in nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var file = 'EoCApp.CT'; | |
if (process.argv.length > 2) | |
file = process.argv[2]; | |
var f = fs.readFileSync(file, 'utf8'); | |
var i = 0; | |
f = f.replace(/<ID>\d+<\/ID>/gm, function () {return '<ID>' + i++ + '</ID>'}); | |
l = file.lastIndexOf('.'); | |
var file2 = file.substring(0, l) + '2.' + file.substring(l + 1); | |
fs.writeFileSync(file2, f); | |
console.log(file2 + " written."); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment