I just published replace, a find-and-replace command line utility that others might find useful. I’m always wanting to replace strings spread throughout my code, and never found an easy way to do it. `find` + `sed` can do it, but the syntax is hard to remember and you don’t get easy-to-read feedback on what replacements are made. It’d be cooler if there were a tool devoted to this.

Replace takes a regular expression string (in JavaScript syntax), a replacement string, and files to search (or directories to search recursively when `-r` is specified). It will print out the lines that have been replaced:

screenshot of replace

There are quite a few other options and output choices, check out the GitHub for more information. It requires installing node.js and its package manager npm if you don’t already have them. Then you can install with:

npm install replace -g

Side note: It’s true that JavaScript is still quite a bit slower than bash/C, so it’s good to question why you’d write something like this in Javascript. I blame how nice it is to write programs in node, how nice it is to handle dependencies and distribute programs with npm, and all the really well-done node libraries out there (like colors for coloring console output). This is something I wouldn’t have taken my free time to write in any other environment.

6 thoughts on “replace

  1. I think that, at least hypothetically, this app won’t be significantly faster written in C than in JavaScript. It’s I/O bound, and in JS regular expressions get JITted to native code (which sed won’t even do!).

    The real slowdown here with our current JS implementations will be overhead for creating and replacing-in strings. This app actually just gave me a crazy idea for how we could optimize this kind of usage, so I wanted to say thanks!

      1. (Weird, I can’t reply to the comment nested one deeper than this.)

        All those regexps will get compiled into the same native code blob. The difference that we’re seeing is this: when we see a regexp literal while parsing a script we actually stop and build the regexp before continuing with parsing. When you run replace with a string-and-flag in a loop (like I assume the test runner is doing) we have to build the regexp over and over again.

        If you’re not executing it in a loop, they’ll have the same performance. If I just stop being lazy and finish doing science on the regexp cache ( ) the performance will be equivalent in the test runner as well.

        Also notable… when you don’t use flags or funky search/replace strings, like str.replace(‘simple’, ‘plain’) we’ll actually do a “flat match”, where we don’t even have to build a regular expression. In that case we can just do a strstr like thing. Extending that to work with the global match flag is also on the docket, and is, in fact, marked as a good first JS engine bug! ( )

  2. I like that it spits out the replacements that it did.

    The way I tend to do this kind of task is to use perl’s -i switch (in-place editing), like `perl -i -pe ‘s/oldstring/newstring/g’ theFiles IWant toEdit`.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s