January 26, 2016

MacOS sed “Invalid Command Code”

The version of sed that is shipped with OS X/MacOS is different to that of most Linux distros in such that you have to issue an extra few characters to get it to do the same command.

If you do the following you will result in a “invalid command code” error when trying to delete line 39 of filenamehere.log:

sed -i 39d filenamehere.log

The OS X/MacOS version needs a value for the -i flag so you don’t inadvertently overwrite a file if you don’t mean to. To overwrite the file we’re trying to modify you just need to add two speech marks after the -i flag as follows.

sed -i "" 39d filenamehere.log

If you wanted to save a new file before overwriting the old one, you’d do the following:

sed -i "filename.log.bak" 39d filenamehere.log

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

css.php