Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SED command error on MACOS X

Tags:

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

like image 353
user2067030 Avatar asked Feb 13 '13 04:02

user2067030


People also ask

Does sed work on Mac?

We know that the sed on Mac OS is the POSIX sed, which cannot use many options. On the other hand, GNU sed is very convenient. For example, GNU sed interprets escape sequences like \t , \n , \001 , \x01 , \w , and \b . OSX's sed and POSIX sed only interpret \n (but not in the replacement part of s ).

What can I use instead of sed?

Alternatives to sed include the Unix utility awk , perl , and nearly any general purpose programming language. awk excels at processing record-based text where each line is thought of as a record composed of columns with similar formatting, perhaps separated by spaces or tabs.

What is sed in command line?

SED is a text stream editor used on Unix systems to edit files quickly and efficiently. The tool searches through, replaces, adds, and deletes lines in a text file without opening the file in a text editor. Learn how to use the sed command and its options through easy-to-follow examples.


1 Answers

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.

like image 180
Eric Boehs Avatar answered Nov 02 '22 05:11

Eric Boehs