24

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

ChrisR
  • 14,370
  • 16
  • 70
  • 107
user2067030
  • 754
  • 1
  • 10
  • 27

5 Answers5

37

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.

Community
  • 1
  • 1
Eric Boehs
  • 1,327
  • 13
  • 17
18

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

erikzenker
  • 752
  • 5
  • 18
  • 3
    Alternatively, you can install `gnu-sed` with the option `--with-default-names` at the end, so the installed gnu-sed will have the `sed` name. – Samuel Li Dec 24 '16 at 00:45
6

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

shellter
  • 36,525
  • 7
  • 83
  • 90
  • 1
    Tested on Mac OS X; it works, though the blank line is simply an empty command (to embed newlines in the text, you have to end the line with a backslash). – Jonathan Leffler Feb 13 '13 at 04:35
  • I try that and I got the following message: extra characters after \ at the end of i command – user2067030 Feb 13 '13 at 04:51
  • if you're using the `vi` or `vim` editor, turn on the list cmd, i.e. `:set list`, and I bet you see white-space characters after your `\\` char. You can't use a word processor to write a `sed` script ; -) !!! good luck. – shellter Feb 13 '13 at 05:25
  • I am using the MACOSX TERMINAL – user2067030 Feb 13 '13 at 11:47
  • @JonathanLeffler Thanks for that; it's implied by what you say, but to be explicit: don't end the *last* (or only) line of the text with \ - only needed for the *interior* lines of *multi-line* text. – mklement0 Dec 22 '13 at 18:30
5

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
Dylan
  • 3,658
  • 1
  • 21
  • 12
2

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

sed '1 i VISPATH=/mnt/local/gdrive/public/3DVis' 
Stephen Niedzielski
  • 2,497
  • 1
  • 28
  • 34