0

I have a large directory tree structure with text files (php, html, etc). I need to search for a string that occurs frequently throughout these files and flip the \ to a /. I need this to recursively traverse the entire structure. I've seen a couple of suggestions about how to do this, but I'm not able to get them to work for me.

Search for: \MySite\scripts\connection.php

Replace with: /MySite/scripts/connection.php

I tried this, but it didn't work:

find ~/Desktop/administration/ -type f -exec grep -l '\MySite\scripts\connection\\' {} \; -exec sed -i 's#\MySite#\scripts#\connection.php\#/MySite#/scripts#/connection/#g' {} \;
AndroidDev
  • 20,466
  • 42
  • 148
  • 239

2 Answers2

0

grep is innecesary, and you have too many #

find ~/Desktop/administration/ -type f -exec sed -i '' 's#\\MySite\\scripts\\connection.php#\/MySite\/scripts\/connection.php#g' {} \;
Raul Andres
  • 3,766
  • 15
  • 24
  • Thanks. I get this error: `sed: -i may not be used with stdin` – AndroidDev Feb 14 '14 at 07:03
  • `-i` is for in-file changes. Are you on a mac? If so do `find ~/Desktop/administration/ -type f -exec sed -i '.bak' 's#\MySite\scripts\connection.php#/MySite/scripts/connection.php#g' {} \;` – jaypal singh Feb 14 '14 at 07:07
  • Upps, `find` should pass file name as parameter to `sed`, so it doesn't reads from `stdin`, and it seems to work for me in Linux. @jaypalsingh comment should be useful if in Mac http://stackoverflow.com/questions/4247068/sed-command-failing-on-mac-but-works-on-linux – Raul Andres Feb 14 '14 at 07:11
  • @jaypalsingh I get this error: `sed: RE error: illegal byte sequence`. – AndroidDev Feb 14 '14 at 07:32
  • illegal byte sequence may be caused for non UTF-8 files. Do you skip binaries. Do ASCII Files work as expected? – Raul Andres Feb 14 '14 at 08:18
  • There was a problem with \ and bash. It seems that a double scape is necessary, as in `echo "a\\b" | sed "s#a\\\\b#a/b#g"` – Raul Andres Feb 14 '14 at 08:30
0

if sed -i option is not available, it works with simple script.

for file in `find ~/Desktop/administration/ -type f`
do
    sed 's#\MySite\scripts\connection.php#/MySite/scripts/connection.php#g' "$file" > tmp_file && mv tmp_file "$file"
done
Raul Andres
  • 3,766
  • 15
  • 24
Fidel
  • 977
  • 1
  • 6
  • 13
  • Thank you, but this doesn't work, either. I get two different repeated errors: `mv: tmp: No such file or directory` and `sed: RE error: illegal byte sequence`. – AndroidDev Feb 14 '14 at 07:49
  • It seems like your filename may contains some special character. can u please check. – Fidel Feb 14 '14 at 08:27