3

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?

user187195
  • 31
  • 2
  • 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. http://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: http://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 03:26

2 Answers2

1

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
sparkie
  • 2,238
  • 1
  • 11
  • 11
1

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
glenn jackman
  • 25,463
  • 6
  • 46
  • 69