sed command with -i option failing on Mac, but works on Linux
If you use the -i
option you need to provide an extension for your backups.
If you have:
File1.txt
File2.cfg
The command (note the lack of space between -i
and ''
and the -e
to make it work on new versions of Mac and on GNU):
sed -i'.original' -e 's/old_link/new_link/g' *
Create 2 backup files like:
File1.txt.original
File2.cfg.original
There is no portable way to avoid making backup files because it is impossible to find a mix of sed commands that works on all cases:
sed -i -e ...
- does not work on OS X as it creates-e
backupssed -i'' -e ...
- does not work on OS X 10.6 but works on 10.9+sed -i '' -e ...
- not working on GNU
Note Given that there isn't a sed command working on all platforms, you can try to use another command to achieve the same result.
E.g., perl -i -pe's/old_link/new_link/g' *
Sed command works on Linux, but not on OS X
For the Mac OS X sed, use -E
instead of -r
to get EREs. Additionally, the GNU extensions aren't there, so you'll need literal characters instead of the \t
and \n
metacharacters.
Or just install GNU sed, of course.
sed in-place flag that works both on Mac (BSD) and Linux
If you really want to just use sed -i
the 'easy' way, the following DOES work on both GNU and BSD/Mac sed
:
sed -i.bak 's/foo/bar/' filename
Note the lack of space and the dot.
Proof:
# GNU sed
% sed --version | head -1
GNU sed version 4.2.1
% echo 'foo' > file
% sed -i.bak 's/foo/bar/' ./file
% ls
file file.bak
% cat ./file
bar
# BSD sed
% sed --version 2>&1 | head -1
sed: illegal option -- -
% echo 'foo' > file
% sed -i.bak 's/foo/bar/' ./file
% ls
file file.bak
% cat ./file
bar
Obviously you could then just delete the .bak
files.
sed -i command for in-place editing to work with both GNU sed and BSD/OSX
OS X sed
handles the -i
argument differently to the Linux version.
You can generate a command that might "work" for both by adding -e
in this way:
# vv
sed -i -e 's|\(.*\)\.o:|$(OBJ_DIR)/\1.o $(OBJ_DIR)/\1.d $(TEST_OBJ_DIR)/\1_utest.o:|' $@
OS X sed -i
interprets the next thing after the -i
as a file extension for a backup copy of the in-place edit. (The Linux version only does this if there is no space between the -i
and the extension.) Obviously a side affect of using this is that you will get a backup file with -e
as an extension, which you may not want. Please refer to other answers to this question for more details, and cleaner approaches that can be used instead.
The behaviour you see is because OS X sed
consumes the s|||
as the extension (!) then interprets the next argument as a command - in this case it begins with t
, which sed
recognizes as a branch-to-label command expecting the target label as an argument - hence the error you see.
If you create a file test
you can reproduce the error:
$ sed -i 's|x|y|' test
sed: 1: "test": undefined label 'est'
unable to use sed command in macos?
If you just want to append a line to your .bash_profile, what about
echo "PATH=\"$PWD:$PATH\"" >> $HOME/.bash_profile
?
And, if you want to be sure to insert only once:
grep -q -x -F 'PATH=\"$PWD:$PATH\"' $HOME/.bash_profile || echo "PATH=\"$PWD:$PATH\"" >> $HOME/.bash_profile
Related Topics
Catch Signal in Bash But Don't Finish Currently Running Command
Tail Logback Log Files with Log Level Coloring in Linux Server
Running Meteor Application on a Single Core
Execute External Program with Trigger in Postgres 9.4
Removing File from Folder in Linux
Calculator in Assembly Language - Linux X86 & Nasm - Division
.Lis Files Generated While Compiling Pro*C Code in Linux
Bash Command Line Arguments Passed to Sed via Ssh
How to Remove Everything Else in a Folder Except Filea
How to Tell Linux to Keep a Page and Not Evict It
Linux Kernel Code in Memory Check with Sha256 Sum
Unzip in Current Directory While Preserving File Structure
Xmonad: Spawnon Workspace That Had Focus When Spawn Key Was Pressed