Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sed command works on Linux, but not on OS X

Tags:

linux

macos

sed

I am using this sed command on Linux, to patch one file, and it works fine:

sed -i -r "s/(\tpublic function __call.*)/\1\n\t\treturn null;/" rb.php

But when I try this command on OS X, I am getting an error:

sed: 1: "s/(\tpublic function __ ...": \1 not defined in the RE

So, can anybody help me to make command that will work both on Linux and OS X?

By the way, I tried command like:

sed -i '' -r "s/(\tpublic function __call.*)/\1\n\t\treturn null;/" rb.php

but it doesn't work on Linux.

like image 518
Ivica Avatar asked Oct 27 '11 09:10

Ivica


People also ask

Does sed work in macOS?

One way to make the GNU version of the SED to work on the Mac OS X, is to directly install the gnu-sed along with the default names which will assure you that you won't have to run different commands on both the operating systems.

What is sed command in Mac?

SED is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits, SED works by making only one pass over the input(s), and is consequently more efficient.

Is Mac OS X based on Linux?

You may have heard that Macintosh OSX is just Linux with a prettier interface. That's not actually true. But OSX is built in part on an open source Unix derivative called FreeBSD.

Is sed a Linux command?

The SED command in Linux stands for Stream EDitor and is helpful for a myriad of frequently needed operations in text files and streams. Sed helps in operations like selecting the text, substituting text, modifying an original file, adding lines to text, or deleting lines from the text.


1 Answers

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.

like image 74
Michael J. Barber Avatar answered Oct 01 '22 12:10

Michael J. Barber