sed: 1: “x.log”: extra characters at the end of x command

Writing by on Tuesday, 26 of May , 2015 at 5:25 am

While using sed in-place edit through sed -i on MacOS X, you will run into the following error.

This will ERROR out

mac:tmp shivdev$ sed -i “s/Apr/May/g” x.log
sed: 1: “x.log”: extra characters at the end of x command

Using an empty extension as shown below Will PASS

mac:tmp shivdev$ sed -i “” “s/Apr/May/g” x.log

The reason that it works fine on Ubuntu but not on MacOS is that it uses a BSD sed (not GNU sed like Ubuntu)

-i extension
Edit files in-place, saving backups with the specified extension.
If a zero-length extension is given, no backup will be saved. It
is not recommended to give a zero-length extension when in-place
editing files, as you risk corruption or partial content in situ-
ations where disk space is exhausted, etc.

Leave a comment

Category: Linux,Mac

Shivdev Kalambi's Blog

Shivdev Kalambi is a Software Development Manager, previously a Principal Software Engineer at ArcSight/HP. With over 16 years' experience in software development, he's worked on several technologies and played different roles and contributed to all phases of projects. Non-tech activies include Ping-pong, Rock Climbing and Yoga at PG, Golf, Skiing, Swimming & a beer enthusiast.