shelljs / shx

Portable Shell Commands for Node
MIT License
1.72k stars 44 forks source link

sed does not replace in files #158

Closed FredLackeyOfficial closed 5 years ago

FredLackeyOfficial commented 5 years ago

No combination of regex or separate strings works for replacing a string in a series of files.

My goal is to change this file...

module.exports = function (sequelize, DataTypes) {
  return sequelize.define('address_line', {
  });
};

... to this ...

module.exports = (sequelize, DataTypes) => {
  return sequelize.define('address_line', {
  });
};

The command I'm currently using is:

shx sed -i 's/function (sequelize, DataTypes)/(sequelize, DataTypes) =>/g' ./data/models/**/*.js

It is currently used within the package.json file of a Node project:

{
  "scripts": {
    "models:fix:function": "shx sed -i 's/function (sequelize, DataTypes)/(sequelize, DataTypes) =>/g' ./data/models/**/*.js"
  }
}

Please explain the correct syntax.

nfischer commented 5 years ago

You need to escape the ( and ) in the regex part.

shx sed 's/function \(sequelize, DataTypes\)/(sequelize, DataTypes) =>/g' ...

shx sed isn't quite a drop-in replacement for unix sed. Contributions are welcome, however.

nfischer commented 5 years ago

^ Closing for now, since I think I've answered your question. For the time being, I think it's easier to just live with shx sed's quirks until we can make a more-complete shell.sed() in ShellJS.