Task

Replace every occurring instance of a piece of text in a group of text files with another one.



For this task we want to replace the text   "Goodbye London!"   with   "Hello New York!"   for a list of files.




/* Global replace in Jsish */
if (console.args.length == 0) {
    console.args.push('-');
}
 
/* For each file, globally replace "Goodbye London!" with "Hello New York!" */
var fn, data, changed;
for (fn of console.args) {
    /* No args, or an argument of - uses "stdin" (a special Channel name) */
    if (fn == 'stdin') fn = './stdin';
    if (fn == '-') fn = 'stdin';
    try {
        data = File.read(fn);
        /* Jsi supports the m multiline regexp flag */
        changed = data.replace(/Goodbye London!/gm, 'Hello New York!');
        if (changed != data) {
            if (fn == 'stdin') fn = 'stdout'; else fn += '.new';
            var cnt = File.write(fn, changed);
            puts(fn + ":" + cnt, 'updated');
        }
    } catch(err) { puts(err, 'processing', fn); }
}