Coder Social home page Coder Social logo

replace's Introduction

replace

replace is a command line utility for performing search-and-replace on files. It's similar to sed but there are a few differences:

Install

With node.js and npm:

npm install replace -g

You can now use replace and search from the command line.

Examples

Replace all occurrences of "foo" with "bar" in files in the current directory:

replace 'foo' 'bar' *

Replace in all files in a recursive search of the current directory:

replace 'foo' 'bar' . -r

Replace only in test/file1.js and test/file2.js:

replace 'foo' 'bar' test/file1.js test/file2.js

Replace all word pairs with "_" in middle with a "-":

replace '(\w+)_(\w+)' '$1-$2' *

Replace only in files with names matching *.js:

replace 'foo' 'bar' . -r --include="*.js"

Don't replace in files with names matching *.min.js and *.py:

replace 'foo' 'bar' . -r --exclude="*.min.js,*.py"

Preview the replacements without modifying any files:

replace 'foo' 'bar' . -r --preview

See all the options:

replace -h

Search

There's also a search command. It's like grep, but with replace's syntax.

search "setTimeout" . -r

Programmatic Usage

You can use replace from your JS program:

var replace = require("replace");

replace({
  regex: "foo",
  replacement: "bar",
  paths: ['.'],
  recursive: true,
  silent: true,
});

More Details

Excludes

By default, replace and search will exclude files (binaries, images, etc) that match patterns in the "defaultignore" located in this directory.

On huge directories

If replace is taking too long on a large directory, try turning on the quiet flag with -q, only including the necessary file types with --include or limiting the lines shown in a preview with -n.

What it looks like

replace

replace's People

Contributors

bericp1 avatar danc86 avatar harthur avatar jeremyckahn avatar max-mapper avatar ncolgan avatar niftylettuce avatar panuhorsmalahti avatar pose avatar sc0ttkclark avatar sopo-to-iki avatar tmcw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

replace's Issues

Programmatically using Replace - correctly finds the search term but doesn't replace

Hi, I'm using the JavaScript code to find and replace some strings. From the results in the console I can see that it is correctly finding two instances, however, the files remain unchanged. I set preview to false just incase it was true by default. But still the string is not being replaced. Have I got something wrong with the call?

var WPpaths = projectDir + '/app/wp-content/themes/' + siteName + '/assets/scss';
replace({
    regex: 'Domain',
    replacment: 'New domain',
    paths: [WPpaths],
    recursive: true,
    count: true,
    preview: false
});

Script stops on permission errors

Heather,
Thank you for writing this module, it helped me a lot today.

I noticed that while running the script on a site with dynamic generated files (cache), some of them were not writable and then instead of skipping those files, it just stopped the script and you would have to either manually set the script to ignore them or change its permissions.

A simple error handler would be nice, you could always log it right next to the file, change its color to red and display the error next to it.

Thanks.

Add directory warning when calling without recursive flag.

When you do replace foo bar dir and leave out the -r, replace should warn you or mark it in some way.

Hopefully it wouldn't be too distracting, as entering replace foo bar dir/* might expand to a bunch of subdirectories. replace wouldn't know the difference and would print out a warning for each of these.

Replacing numbers only causes "undefined"

I am using [email protected] to replace part of a phone number.

I have tried a few variations

replace 383 343 -rp .
replace "383" "343" -rp .

It results in

<p>By Phone: <a href="tel:+1888undefined8400">888.undefined.8400</a>

If I add a non digit character to the replacement text, it seems to pick it up correctly.

replace 383 343a -rp .
<p>By Phone: <a href="tel:+1888343a8400">888.343a.8400</a>

Perhaps the numbers aren't parsing correctly? I will take a look when I have some time but wanted to report the issue promptly.

Add license text

Can you please add a copy of the license text to the source repo (and NPM package)?

Right now there is no indication of any license except the NPM metadata which says "MIT".

Multiline use

I wan't to replace with multiline a

tag which goes over multiple lines:

replace '<p>(.*?)</p>' '<pre>$1</pre>' index.html -p -m

How is the correct syntax to do this? The above call only works if the whole tag is in one line.

package.json expresses nomnom 1.0.x dep, but programs against more recent API

nomnom.options is apparently not a thing in 1.0.x and so attempts to use bin/replace.js fail. But if I relax the package.json to 1.x and then force an upgrade and get 1.5, things seem to work. I'm not sure what dep you would want, so no pull request.

Thanks for the great tool! Batch replacing has always been one of my command-line nightmares and my sed-fu is weak.

Minimatch outdated

The minimatch version used is < 0.3.0, and then subject to vulnerability...
Could you please update the package.json to latest minimatch version ?

Thanks.

Set file encoding

I have a file which encoding is UTF-16. Replacements in this file doesn't work because 'replace' assumes file encoding is UTF-8. Would it be possible to add an 'encoding' option to set the file encoding of the matched files ?

Is this project dead?

No commit in 3.5 years.
One dependency is out of date and has vulnerabilities (minimatch).
Another is deprecated entirely (nomnom).
Issues untouched for years.
Any chance this will see an update?

Replace all

How to replace all content of a file with same text ?

/usr/local/bin/replace clashes with mariadb

❯ sudo npm install replace -g
Password:
npm http GET https://registry.npmjs.org/replace
npm http 304 https://registry.npmjs.org/replace
npm http GET https://registry.npmjs.org/nomnom
npm http GET https://registry.npmjs.org/colors
npm http GET https://registry.npmjs.org/minimatch
npm http 200 https://registry.npmjs.org/colors
npm http GET https://registry.npmjs.org/colors/-/colors-0.5.1.tgz
npm http 200 https://registry.npmjs.org/nomnom
npm http GET https://registry.npmjs.org/nomnom/-/nomnom-1.6.2.tgz
npm http 200 https://registry.npmjs.org/minimatch
npm http 200 https://registry.npmjs.org/colors/-/colors-0.5.1.tgz
npm http 200 https://registry.npmjs.org/nomnom/-/nomnom-1.6.2.tgz
npm http GET https://registry.npmjs.org/minimatch/-/minimatch-0.2.14.tgz
npm http 200 https://registry.npmjs.org/minimatch/-/minimatch-0.2.14.tgz
npm http GET https://registry.npmjs.org/underscore
npm http GET https://registry.npmjs.org/lru-cache
npm http GET https://registry.npmjs.org/sigmund
npm http 200 https://registry.npmjs.org/sigmund
npm http GET https://registry.npmjs.org/sigmund/-/sigmund-1.0.0.tgz
npm http 200 https://registry.npmjs.org/lru-cache
npm http GET https://registry.npmjs.org/lru-cache/-/lru-cache-2.5.0.tgz
npm http 200 https://registry.npmjs.org/underscore
npm http GET https://registry.npmjs.org/underscore/-/underscore-1.4.4.tgz
npm http 200 https://registry.npmjs.org/sigmund/-/sigmund-1.0.0.tgz
npm http 200 https://registry.npmjs.org/underscore/-/underscore-1.4.4.tgz
npm http 200 https://registry.npmjs.org/lru-cache/-/lru-cache-2.5.0.tgz
/usr/local/bin/search -> /usr/local/lib/node_modules/replace/bin/search.js
npm ERR! error rolling back Error: Refusing to delete: /usr/local/bin/replace not in /usr/local/lib/node_modules/replace
npm ERR! error rolling back     at clobberFail (/usr/local/lib/node_modules/npm/lib/utils/gently-rm.js:41:12)
npm ERR! error rolling back     at next (/usr/local/lib/node_modules/npm/lib/utils/gently-rm.js:27:14)
npm ERR! error rolling back     at /usr/local/lib/node_modules/npm/lib/utils/gently-rm.js:36:12
npm ERR! error rolling back     at Object.oncomplete (fs.js:107:15)
npm ERR! error rolling back  [email protected] { [Error: Refusing to delete: /usr/local/bin/replace not in /usr/local/lib/node_modules/replace] code: 'EEXIST', path: '/usr/local/bin/replace' }
npm ERR! Refusing to delete: /usr/local/bin/replace not in /usr/local/lib/node_modules/replace
File exists: /usr/local/bin/replace
Move it away, and try again.

npm ERR! System Darwin 13.0.0
npm ERR! command "node" "/usr/local/bin/npm" "install" "replace" "-g"
npm ERR! cwd /Users/psynaptic/repos/github
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.3.24
npm ERR! path /usr/local/bin/replace
npm ERR! code EEXIST
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/psynaptic/repos/github/npm-debug.log
npm ERR! not ok code 0
❯ ls -l /usr/local/bin/replace
lrwxr-xr-x  1 psynaptic  admin    36B 12 Feb 14:56 /usr/local/bin/replace -> ../Cellar/mariadb/5.5.35/bin/replace

How to run test properly?

I've installed tape, and did what the readme told me to.

However when I ran tap, I got something like:

"node" "memory-leaks", and cannot find module "weak"

replace: No to-string for last from-string

The following command returns "replace: No to-string for last from-string"

replace 'Person' 'API.IPerson' . -r --include="*.ts"

According to the docs this should work. Any ideas?

Confirm flag

Would love to have something like vim's /gc mode in which replace would ask whether to replace each segment and get a y/n answer. I'm game to implement this if it's desirable.

awesome utility, but...

This is great, and a huge help.

But please, oh please, for the love of all things good and right in this world, don't use "replace 'var' with 'let' in JS files" as your primary examples. "Let is the new var" is one very dangerous piece of misinformation that is starting to spread, and any time I see it, it makes me want to throw up a little in my mouth. :)

TypeError: path must be a string

When file name (in this example - "1") consists of decimal digits, replace crashes:

(shell) $ replace.js a b 1 -p

fs.js:679
  return binding.lstat(pathModule._makeLong(path));
                 ^
TypeError: path must be a string
    at Object.fs.lstatSync (fs.js:679:18)
    at replacizeFileSync (replace/replace.js:110:22)
    at module.exports (replace/replace.js:53:13)
    at Object.<anonymous> (replace/bin/replace.js:55:1)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)

not working command

I'm using windows 10.
and trying to replace 4 space to Tab with command like this

$ replace "    " "\t" . -r

but nothing changed..

Results from JS API

If you are calling replace as a function it would be cool to get information back about what was replaced as it runs or after it completes.

Perhaps return a results object or take an options callback that reports back file and occurrence information as it runs.

Output scrambles terminal in Yosemite

Hi @harthur , thanks for this very nice tool. I've been using it for some refactoring work and I'm noticing that in our repo (granted, it has a very large Godeps directory so that may be part of the problem) I can consistently duplicate a bug which scrambles the output of the terminal.

Try these steps:

$ git clone https://github.com/docker/machine
$ cd machine
$ replace "github.com/docker/machine/drivers" "github.com/docker/machine/libmachine/drivers"

I get a really crazy looking output:

screen shot 2015-08-21 at 10 42 55 am

And my terminal is now completely scrambled:

screen shot 2015-08-21 at 10 53 06 am

I'm using iTerm2 on Yosemite. Not a show-stopping bug, but I figured I'd report it! Thanks.

request for contextual address functionality

sed allows for a contextual address(regex) which selects all of the pattern spaces that match the address. A command with no address selects every pattern space.

So, in sed you can do this:

sed -n '/styles-min/p' file.html

which results in say,

<link rel="stylesheet" href="assets/css/styles-min.2ac224ec.css" />

This is equivalent to

search 'styles-min' file.html

You can also do this with sed:

sed -n '/styles-min/ s/\.css/.min.css/p' file.html

yielding:

<link rel="stylesheet" href="assets/css/styles-min.2ac224ec.min.css" />

There is no equivalent in replace that does this to my knowledge. Would something like this be possible? Something to the effect of

srep 'styles-min' '\.css' '.min.css' file.html
or 
srep  '\.css' '.min.css' --c 'styles-min' file.html

Add maintainers and revamp project

Hi @harthur, this project is great! I've used it for years now and would love to help keep it alive. Would you consider removing this from "UNMAINTAINED" mode and adding others like me as contributors? I'd be happy to help with the workload. Reach out to me at the following email address if you want to chat more first. 👌

// js
'dawson' + ('drofstob'.split('').reverse('').join('')) + '@gmail.com'

replace with new line '\r\n'

Is there a way to replace with special chars rather than string? I did a replace on a list of emails looking something like replace ';' '\r\n' email.txt This did do the replace but replaced it with a visible \r\n rather than a new line. So to reverse it I thought I'd do the reverse replace '\r\n' ';' email.txt but this actually converted all my line breaks into ; instead. I see now that there's a dry run option which I will run next time but is there a way to replace with a line break? Cheers

How to use the asynchronous way to replace content?

Is there any way to replace content in an asynchronous way?
I didn't find the async api in README.md, but found an async option in replace.js which seems can asynchronously read and write file.
However, how can I catch the replaceComplete event?

`replace` a keyword in windows cmd.exe

Hi! Lovely util. Without all the drama I would've never come across it.

Now, I tried using it on Windows, and got really weird behaviour, until I realized that there's actually a cmd.exe command called "replace". I solved it by copying .../npm/replace.cmd to .../npm/replacejs.cmd. Maybe it would be good to include that alias by default? Or, if npm allows, by default on Windows systems?

replace not always working when regex string contains non-alphanumeric characters (Programmatic Usage))

Hi,
First of all thanks for making this useful package, I have been using it extensively ever since I found it.
This extensive use has uncovered an issue for me that I would like to share. I noticed that when I used the programmatic usage like so:
replace({ regex: "foo", replacement: "bar", paths: ['.'], recursive: true, silent: true, });
'foo' did not get replaced by 'bar' when 'foo' contained (in my case) the characters : + ( ) even through the file contained the exact same string (it was a Date().toString() object with spaces replaced with underscores). Replacing these four characters with an underscore made it work again (granted it still matched the file string of course). I'm not entirely sure which one of the four provided made it fail, or whether it was all four, but I can imagine there could be other characters that cause this issue to happen too.
I hope this was helpful,
Arnaud

tests fail due to missing test_files/test_numbers.txt

Tests fail as below. Maybe you just forgot to git add test_numbers.txt?

$ tap test
ok test/paths.js ...................................... 19/19
not ok test/sanity.js ................................... 2/3
    Command: "node" "sanity.js"
    TAP version 13
    ok 1 single letter replace works
    ok 2 reverting worked
    not ok 3 test/sanity.js
      ---
        exit:    7
        stderr:  |
          /usr/lib/node_modules/tape/index.js:75
                  throw err
                        ^
          Error: ENOENT, no such file or directory './test_files/test_numbers.txt'
              at Object.fs.lstatSync (fs.js:690:18)
              at replacizeFileSync (/home/dcallagh/work/replace/replace.js:121:22)
              at module.exports (/home/dcallagh/work/replace/replace.js:64:13)
              at Test.file (/home/dcallagh/work/replace/test/sanity.js:39:3)
              at Test.bound [as _cb] (/usr/lib/node_modules/tape/lib/test.js:59:32)
              at Test.run (/usr/lib/node_modules/tape/lib/test.js:72:10)
              at Test.bound [as run] (/usr/lib/node_modules/tape/lib/test.js:59:32)
              at Object.next [as _onImmediate] (/usr/lib/node_modules/tape/lib/results.js:66:15)
              at processImmediate [as _immediateCallback] (timers.js:345:15)
        command: "node" "sanity.js"
      ...

    1..3
    # tests 3
    # pass  2
    # fail  1

total ................................................. 21/22

not ok

replace -h

replace -h does not seem to print out options as suggested in the readme (search -h is fine)?

node v0.6.14

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.