Apply regular expression substitution globally to many files with a script

前端 未结 5 1912
小蘑菇
小蘑菇 2020-12-17 00:58

I want to apply a certain regular expression substitution globally to about 40 Javascript files in and under a directory. I\'m a vim user, but doing this by hand can be ted

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-17 01:27

    You can automate the actions of both vi and ex by passing the argument +'command' from the command line, which enables them to be used as text filters.

    In your situation, the following command should work fine:

    find /path/to/dir -name '*.js' | xargs ex +'%s/,\(\_\s*[\]})]\)/\1/g' +'wq!'

提交回复
热议问题