0

I have hundreds of sources codes files. I want to rename namespace according to the filenames path. Of course slashes must be replace by dots.

I have tried with find -exec but I failed to get the filename inside sed to replace the current namespace with the path.

find -type f -iname *.cs  -exec  sh -c 'x="{}"; sed -E "s/\//./; s/(namespace.Project).+/\1.$x/" {}' \;

But I get

sed: -e expression #1, char 36: unknown option to `s'

JDoe
  • 1
  • Please view https://unix.stackexchange.com/questions/156008/is-it-possible-to-use-find-exec-sh-c-safely – pLumo May 22 '19 at 12:41
  • @pLumo please post that as an answer. When you post answers as comments, the question remains marked as unanswered. You also circumvent the normal working of the site since your answer can't be voted on. – terdon May 22 '19 at 13:13
  • @jdoe, please [edit] your question and show us a few examples of the output of find -type f -iname *.cs and then show us what you want to do to those files. Do you actually want to rename them? Do you just want to print them? Do you only want to change / to .? We can't really help unless you show us exactly what you need. – terdon May 22 '19 at 13:15

2 Answers2

0

The first sed command (s/\//./) will replace all slashes with a dot in the file content, not in the file name.

The second sed command (s/(namespace.Project).+/\1.$x/) will add $x (the still unaltered file path) to the namespace. This produces an error in sed because it contains unescaped slashes which are interpreted by sed.

See this related question.


Also, please read on how to use find -exec sh -c safely. Adding {} directly to the bash content is a security nightmare.

pLumo
  • 22,565
0

well I find my way… with bash variables. Something like this:

for i in `find -type f -iname *.cs`; do j=$(echo $i|sed -E 's/\.\//namespace Project\//g;s/\//./g; s/(\.[[:alnum:]]*\.cs$)//g'); sed -i -E "s/(namespace Project.+)/$j/g" $i ; done;
JDoe
  • 1