OS X + Sed: “extra characters at the end of l command” Error

PROBLEM

When executing the following command on OS X…

sed -i 's/megatron/pony/g' /path/to/file.txt

… the following error occurs:-

sed: 1: "/path/to/file.txt": extra characters at the end of l command

SOLUTION

Unlike Ubuntu, OS X requires the extension to be explicitly specified. The workaround is to set an empty string:-

sed -i '' 's/megatron/pony/g' /path/to/file.txt

… and now, Megatron has transformed into a pony.

8 thoughts on “OS X + Sed: “extra characters at the end of l command” Error

  1. Pingback: replace string across multiple files with sed | Yunming Zhang's Blog

  2. Asle

    Thanks! Got the same issue with error “sed: 1: “hello.txt”: extra characters at the end of h command” so adding the empty ” solved that.
    sed -i ” ‘s/FOO/BaR/g’ hello.txt

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s