GNU sed running on Mac

Super basic question. I installed sed GNU on a MAC running High Sierra. However, when I run sed '1i>sometext , I get the following error:

sed:  1:  "1isometext"; command i expects \ followed by text

I have added the \ with no success. Is there anyway I can run sed and awk on MAC in the same way I do in linux?

The absence of a second single quote makes it difficult to guess what exactly you want to do - a redirection, or inserting a <-gt> sign plus sometext?
Howsoever, for insertion, the man page says sed wants a "backslash <new line>" combination - try this and report back.

sorry! My bad, I meant :

sed '1isometext' infile

Or even

echo a | sed '1isometext'

return the same error on MAC:

sed:  1:  "1isometext": command i expects \ followed by text

I can run either command in CygWin and add the "sometext" line to the first line in the infile no problem. I am trying to run all my sed scripts in MAC without any modifications

As per the OSX sed manual (and the error message), you need to use a backslash and newline for a, i and c:

[1addr]i\
     text
             Write text to the standard output.

So it should be something like this:

sed '1i\
sometext'

Using strings $(which sed) | grep "followed by text" on GNU sed returns nothing, whereas it does on macOS's standard sed.

So, even though you installed GNU sed, is that the one you are actually using? e.g. is that the first one you would find based on your PATH variable?

That was the problem actually.
Thanks!

1 Like