Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am trying to do a recursive find and replace in HP-UX and am missing something.

What I have at the moment:

find . -name "*.java" | xargs sed 's/foo/bar/g'

I know the problem is that it's not replacing the file inline. I believe on other OS's there is a -i flag, but it seems in my HP-UX version, there isn't.

Any suggestions?

share|improve this question
1  
In-place editing for sed is not POSIX, which explains why it's not available for you. You could try Perl instead if it's available, since Perl has a -i option that does the same. backreference.org/2011/01/29/in-place-editing-of-files has some info. Just don't blindly try something without backup! –  Daniel Andersson Jan 11 '13 at 13:03
1  
Thanks Daniel. Using perl was the way that worked for me: find . -name "*.java" | xargs perl -pi -e's/foo/bar/g' –  user187195 Jan 11 '13 at 13:08
    
Also asked on StackOverflow: stackoverflow.com/q/14278552/7552 –  glenn jackman Jan 11 '13 at 15:50
    
just take out the xargs and leave the sed. –  Tim Kennedy Jan 14 '13 at 3:26

2 Answers 2

you could workaround the missing -i like this (untested):

for i in `find . -name "*.java"`; do cp $i /tmp/$$; sed 's/foo/bar/g' < /tmp/$$ > $i;done
share|improve this answer

You could always use ed

find . -name "*.java" | while IFS= read -r file; do
  ed "$file" <<ED_COMMANDS
%s/foo/bar/g
w
q
ED_COMMANDS
done
share|improve this answer

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.