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
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 ).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With