Created
April 17, 2020 13:27
-
-
Save kigiri/9feaa033fb35f83f22bbc7c37d7750e7 to your computer and use it in GitHub Desktop.
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
import { readFileStr, writeFileStr } from 'https://deno.land/std/fs/mod.ts' | |
const ext = `.${Deno.args[0] || 'txt'}`.replace(/^\.\.+/, '.') | |
const filenames = (await Deno.readdir('.')) | |
.map(file => file.name) | |
.filter(name => name.endsWith(ext)) | |
.filter(name => !name.endsWith(`-replaced${ext}`)) | |
const work = filenames.map(async name => { | |
const text = await readFileStr(name) | |
const replaced = text.replace(/\n\d+,\n\d+,/g, '') | |
const count = (text.match(/\n\d+,\n\d+,/g)||[]).length | |
if (text === replaced) return | |
const newName = `${name.slice(0, ext.length)}-replaced${ext}` | |
await writeFileStr(newName, replaced) | |
return { name, newName, count } | |
}) | |
const result = (await Promise.all(work)) | |
.filter(Boolean) | |
const totalOccurences = result | |
.reduce((total, {count}) => total + count, 0) | |
console.log(totalOccurences, 'occurences replaced in', result.length, 'files.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
deno --allow-read=. --allow-write=. "https://gist.githubusercontent.com/kigiri/9feaa033fb35f83f22bbc7c37d7750e7/raw/cece52a9dc86ebd8a480584ffd9fc7fa88312185/replace-numbers.js"