Skip to content

Instantly share code, notes, and snippets.

@kigiri
Created April 17, 2020 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kigiri/9feaa033fb35f83f22bbc7c37d7750e7 to your computer and use it in GitHub Desktop.
Save kigiri/9feaa033fb35f83f22bbc7c37d7750e7 to your computer and use it in GitHub Desktop.
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