Embracing the Messiness in Search of Epic Solutions

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

Posted

in

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.

Tags:

Comments

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

  1. Bob Avatar
    Bob

    Thanks! That worked for FreeBSD 10.2.

  2. nicolas pinson Avatar

    Cheers, appreciated!

  3. pratik patel Avatar

    Great..!
    Thanks

  4. Mark Leavenworth Avatar
    Mark Leavenworth

    Thank you!

  5. Omar Gameel Avatar
    Omar Gameel

    Great tip!
    Thanks

  6. Asle Avatar
    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

  7. oh la la (@trendinhastasi) Avatar

    I can’t still get the following working on macOS Sierra;

    `sed -i ” ‘/^$/{N ; /second/D}’ data-with-empty-lines2.txt`

  8. pratel jakey Avatar
    pratel jakey

    I got it to work but can you explain _why_ that works?

    1. Choon-Chern Lim Avatar

      BSD’s sed (used by Mac) is a little different from GNU’s sed. You can get more info from here. That said, you probably can install GNU’s sed on Mac using Homebrew if you wish.

Leave a Reply