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.

I want to replace the backslash in the string page_path\example_filename.txt with a forward slash. I also want to be able to run this on a large file system and have it recursively search all directories.

I found someone on the web who said to use grep, xargs, and sed but I wasn't able to get it to work. I've been trying different variations on delimiters and escape characters, but didn't get it.

Here is the command I was trying to run:
grep -lr -e 'page_path\\' * | xargs sed -i 's/page_path\/page_path//g'

Thanks in advance.

share|improve this question
    
This might interest you –  Gowtham Mar 21 '12 at 18:11
add comment

2 Answers

up vote 3 down vote accepted

Also you can use find for that:

find /your/path -type f -exec grep -l 'page_path\\' {} \; -exec sed -i 's#page_path\\#page_path/#g' {} \;

The second exec will be executed only if the first one was succeed, and you won't get problems with unprintable or escape needing characters in file names.

share|improve this answer
    
great, thanks for the answer –  Xenocideae Mar 22 '12 at 13:38
add comment

With grep | xargs, you get problems with whitespaces in filenames. You can avoid this problem by using: grep -lZr ... | xargs -0 sed ... which uses zero bytes as delimiter.

And now regarding the sed command: The trick is that you can use instead of s/a/b/ any other character, e.g. s#a#b#:

 sed -i 's#page_path\\#page_path/#g'
share|improve this answer
1  
FYI, grep has a -Z option for null-delimited file names, so grep -Z ... | xargs -0 ... is the generally accepted way to do that. –  Kevin Mar 21 '12 at 18:26
    
@Kevin Thanks, I improved my answer. –  jofel Mar 21 '12 at 18:29
    
cool, didn't know any character could work as a delimiter in sed –  Xenocideae Mar 22 '12 at 13:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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