-2

I have a command like this:

sed -i -e '/console.log/ s/^\/*/\/\//' *.js

which does comments out all console.log statements. But there are two things

  1. It keeps the backup file like test.js-e , I doesn't want to do that.
  2. Say I want to the same process recursive to the folder, how to do it?
batman
  • 3,565
  • 5
  • 20
  • 41
  • possible duplicate of [Awk/Sed: How to do a recursive find/replace of a string?](http://stackoverflow.com/questions/1583219/awk-sed-how-to-do-a-recursive-find-replace-of-a-string) – NeronLeVelu Feb 03 '15 at 08:03

3 Answers3

2

You don't have to use -e option in this particular case as it is unnecessary. This will solve your 1st problem (as -e seems to be going as suffix for -i option).

For the 2nd part, u can try something like this:

for i in $(find . -type f -name "*.js"); do sed -i '/console.log/ s/^\/*/\/\//' $i; done;

Use find to recursively find all .js files and do the replacement.

Arjun Mathew Dan
  • 5,240
  • 1
  • 16
  • 27
  • How comes that will work with a regex `^\/*` to match? won't that just match every line start (`^`)? – armnotstrong Feb 03 '15 at 08:19
  • @armnotstrong: i didn't look at his expression btw... s/^/\/\//' would have been enough (comment out the mathcing line). He's using s/^\/*/\/\//' (see the * there.. => zero or more / at the beginning, which always evaluated to 0 / at beginning) – Arjun Mathew Dan Feb 03 '15 at 08:30
  • I presume that's not what the OP originally want to do. but He accept your answer, anyway, hope he knows what he is doing :-) – armnotstrong Feb 03 '15 at 08:34
0

When checking sed's help, -i takes a suffix and uses it as a backup,

-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)

and the output backup seems to be samefile + -e which is the second argument you're sending, try removing the space and see if that would work

sed -ie '/console.log/ s/^\/*/\/\//' *.js

As for the recursion, you could use find with -exec or xargs, please modify the find command and test it before running exec

find -name 'console.log' -type f -exec sed -ie '/console.log/ s/^\/*/\/\//' *.js \;
Mohammad AbuShady
  • 40,884
  • 11
  • 78
  • 89
0

From your original post I presume you just want to make a C-style comment leading like:

/*

to a double back-slash style like:

//

right? Then you can do it with this command

find . -name "*.js" -type f -exec sed -i '/console.log/ s#^/\*#//#g' '{}' \;

To be awared that:

  • in sed the split character normally be / but if you found that annoying to Escape when your replacing or matching string contains a / . You can change the split character to # or | as you like, I found it very useful trick.

  • if you do want to do is what I presumed, be sure that you should Escape the character *, because a combination of regex /* just means to match a pattern that / occurs one time or many times or none at all, that will match everything, it's very dangerous!

armnotstrong
  • 8,605
  • 16
  • 65
  • 130