Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using sed -i Mac OSX without a backup file

Tags:

macos

sed

I found this post: Variations of sed between OSX and GNU/Linux

SED without backup file

Which provided the solution for using sed -i on Mac OSX such that it does not create a back file on a search and replace command.

E.g., Note the singlequote space singlequote after -i option to specify a zero length extension:

sed -i ' ' 's/foo/|bar/g' test.html

My problem - this DOES create a backup file for me, and it gives the backup file the same name as my input file, "test.html"

I need to run a search/replace on many files, and I don't want backup files.

Here's my actual command:

sed -i ' ' "s|research/projects/vertebrategenome/havana/|science/groups/vertebrate-annotation|g" test2.html

Where I get a backup file called "test2.html"

like image 619
Cath Avatar asked Jan 08 '23 03:01

Cath


2 Answers

On the mac, you get the BSD version of sed. It required to include a null string as an argument to -i. Note, on linux, it is happy to accept the -i with no extension.

On Linux:

sed -i 's|StringToReplace|ReplacedString|'

on MacOSX/*BSD

sed -i '' 's|StringToReplace|ReplacedString|'

Note, you want a nullstring ('') not a blank(' ').

like image 54
Atifm Avatar answered Jan 14 '23 14:01

Atifm


The best way I have found to have the same script work on both Linux and Mac is to:

sed -i.bak -e 's/foo/bar/' -- ${TARGET}
rm ${TARGET}.bak
like image 32
vikrantt Avatar answered Jan 14 '23 14:01

vikrantt