0

So in React 0.12 having /** @jsx React.DOM */ at the top of all jsx files is no longer necessary. A library that I'm using actually throws an error when it sees the /** @jsx React.DOM */ line that exists at the top of every jsx file.

So I wanted to know if there was a quick way using sed to remove this line from every file within a directory.

Should I just remove line 1, or is there a way for me to pattern match, and verify that it's at the top? Also how would one recursively remove it from all files within a directory, and it's subdirectories?

Thank you!

loonyuni
  • 1,373
  • 3
  • 16
  • 24
  • How to in-file replace a match and delete a line can be found here: http://stackoverflow.com/questions/5410757/delete-a-line-containing-a-specific-string-using-sed How to recursively traverse directories is here: http://stackoverflow.com/questions/18897264/bash-writing-a-script-to-recursively-travel-a-directory-of-n-levels – bbuckley123 Oct 02 '15 at 19:01

1 Answers1

4

Yes, it's simple with the help of find. The following command lines the first line of all *.jsx files:

find /path/to/files -name '*.jsx' -exec sed -i '1d' {} \;

This command removes lines containing that certain comment:

find /path/to/files -name '*.jsx' -exec sed -i '/^\/\*\* @jsx React\.DOM \*\/$/d' {} \;

Make a backup of your files before issuing that commands since they modify the files in place. Better safe than sorry.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • 1
    To be 100% safe you should really escape the `*`s and the `.` in the regexp - `/\/\*\* @jsx React\.DOM \*\//` and it MAY even be appropriate to anchor it `/^\/\*\* @jsx React\.DOM \*\/$/` if that's the whole line. – Ed Morton Oct 02 '15 at 20:46
  • @EdMorton Indeed! You are right. I had in mind that `*` when used as a quantifier in `sed`, without passing `-r`, needs to get escaped. I should blame *you* because you showed me to use more `awk` and less `sed`. :) You see, my knowledge about `sed` get's rusty already! – hek2mgl Oct 02 '15 at 20:50