user2067030
user2067030

Reputation: 764

SED command error on MACOS X

I am trying to run this command on MacOSX terminal , which was initially intended to run on Linux

sed '1 i VISPATH=/mnt/local/gdrive/public/3DVis' init.txt >> ~/.bash_profile

but it gives me the error:

command i expects \ followed by text. 

is there any way I could modify the above command to work on MacOSX terminal

Upvotes: 25

Views: 21587

Answers (5)

erikzenker
erikzenker

Reputation: 752

Had the same problem and solved it with brew:

brew install gnu-sed

gsed YOUR_USUAL_SED_COMMAND

If you want to use the sed command, then you can set an alias:

alias sed=gsed

Upvotes: 18

Eric Boehs
Eric Boehs

Reputation: 1327

Shelter is right but there's another way to do it. You can use the bash $'...' quoting to interpret the escapes before passing the string to sed.

So:

         sed -iold '1i\'$'\n''text to prepend'$'\n' file.txt
                      ^^^^^^^^                     ^
                     / |\|||/ \                    |__ No need to reopen
                     | | \|/  |                          string to sed
     Tells sed to    | |  |   |
    escape the next _/ |  |   +-----------------------------+
         char          |  +-------------+                   |
                       |                |                   |
                  Close string   The  special bash   Reopen string to
                     to sed       newline char to      send to sed
                                    send to sed

This answer on unix.stackexchange.com led me to this solution.

Upvotes: 37

Dylan
Dylan

Reputation: 3678

Here's how I worked it out on OS X. In my case, I needed to prepend text to a file. Apparently, modern sed works like this:

sed -i '1i text to prepend' file.txt

But on OS X I had to do the following:

sed -i '' '1i\
text to prepend
' file.txt

Upvotes: 6

shellter
shellter

Reputation: 37278

The OSX seds are based on older versions, you need to be much more literal in your directions to sed, AND you're lucky, in this case, sed is telling you exactly what to do. Untested as I don't have OSX, but try

sed '1 i\
VISPATH=/mnt/local/gdrive/public/3DVis

' init.txt >> ~/.bash_profile

Input via the i cmd is terminated by a blank line. Other sed instructions can follow after that. Note, NO chars after the \ char!

Also, @StephenNiedzielski is right. Use the single quote chars to wrap your sed statements. (if you need variable expansion inside your sed and can escape other uses of $, then you can also use dbl-quotes, but it's not recommended as a normal practices.

edit

As I understand now that you're doing this from the command-line, and not in a script or other editor, I have tested the above, and.... all I can say is that famous line from tech support ... "It works for me". If you're getting an error message

sed: -e expression #1, char 8: extra characters after command

then you almost certainly have added some character after the \. I just tested that, and I got the above error message. (I'm using a linux version of sed, so the error messages are exactly the same). You should edit your question to include an exact cut-paste of your command line and the new error message. Using curly-single-quotes will not work.

IHTH

Upvotes: 6

Stephen Niedzielski
Stephen Niedzielski

Reputation: 2637

It looks like you copied rich text. The single quotes should be straight not curly:

sed '1 i VISPATH=/mnt/local/gdrive/public/3DVis' 

Upvotes: 2

Related Questions