Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Want to replace the words with space in multiple files using SED commmand

I have a word as 'Danny' with single quotes in many of my files in multiple directories which I want to change to 'Danny Samuel'

since I have a space in the new word this command wont work

find . -type f -exec sed -e 's/'Danny'/'Danny Samuel'/g' -i.php '{}' +
share|improve this question

closed as off-topic by devnull, slm, Chris Down, manatwork, Thomas Nyman Apr 17 at 10:21

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – devnull, slm, Chris Down, manatwork, Thomas Nyman
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Remove the quotes from the replacement string. –  devnull Apr 17 at 6:49
1  
If you need to quote the replacement and pattern, use a different set of quotes for the sed expression. –  devnull Apr 17 at 6:50
    
i tried this but doesnt work either find . -type f -exec sed -e 's/Danny/Danny Samuel/g' '{}' + –  Danny Apr 17 at 6:55
    
Could you elaborate does not work? –  devnull Apr 17 at 6:57
    
It doesnot change the Danny to Danny Samuel at all ....I checked the files it still says as Danny itself –  Danny Apr 17 at 6:59

1 Answer 1

up vote 2 down vote accepted

You can't have a single quote inside single quotes, the ' in 'Danny closes the quotes.

find . -type f -exec sed -e 's/'Danny'/'Danny Samuel'/g' -i.php '{}' +
                             ^^ !!!!! ^ !!!!!!!!!!!! ^^
                         quoted     quoted         quoted  
                              unquoted    unquoted

Write it:

find . -type f -exec sed -i.php -e "s/'Danny'/'Danny Samuel'/g" {} +

(note that .php is quite unexpected as an extension for backup files).

share|improve this answer
    
it goes on a loop if I remove the -i.php where as I can still execute the same command and remove the created backup files like file.php.php by this command "find . -name ".php.php" -print0 | xargs -0 rm -rf" –  Danny Apr 17 at 7:36
    
@danny, use -i (GNU sed) or -i '' (FreeBSD, OS/X) if you want in-place editing but no backup. –  Stéphane Chazelas Apr 17 at 12:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.