8

I have a large project where I want to replace module names using the following command:

find app/ -type f -exec sed -i '' 's/Foo/Bar/g' {} +

This works great, but sed also adds newlines to the end of all the files (even if it can't find any Foo's to replace).

How can I prevent sed from adding these newlines?

I'm on OSX, using the BSD version of sed.

(For the record, I very much agree with sed here, but I dont want to pollute the git history of the project.)

4
  • 1
    This behavior (adding a missing newline at EOF) is not found in GNU sed
    – Leon
    Jul 13, 2016 at 15:25
  • I'm on OSX, using the BSD version: developer.apple.com/legacy/library/documentation/Darwin/…
    – Torstein
    Jul 13, 2016 at 15:38
  • Are you saying sed is adding a blank line at the end of the file or that the original file does not have a terminating newline and sed is adding it? If the former your sed is broken, if the latter then your input files are not text files per the POSIX standard so whatever sed or any other UNIX tool does with them is undefined behavior in POSIX.
    – Ed Morton
    Jul 14, 2016 at 2:49
  • 1
    The latter. As I said, I agree with sed, but don't want to pollute the git history .
    – Torstein
    Jul 14, 2016 at 11:51

1 Answer 1

8

Perl to the rescue:

perl -i -pe 's/Foo/Bar/g'

Perl doesn't add newlines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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