Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does sed report "Invalid command code" for tilde?

Tags:

unix

sed

I am running the following sed command. I want to extract every 4th line starting at the second line:

input:
$ sed -n '2~4p' filename

output:
sed: 1: "2~4p": invalid command code ~

Does anyone know why this is occurring? I am using mac OSX 10.8.

I know the following awk command command will do: $ awk "NR%4==2 {print}"

I'm curious why tilde is invalid for this sed command.

like image 602
cebach561 Avatar asked Mar 11 '14 16:03

cebach561


1 Answers

I guess you're in Mac OS X. It has the BSD implementation of sed and if you search for ~ in the man page you'll find nothing, because it's not there.

You can install the GNU implementation, for example with MacPorts:

sudo port install gsed

or Brew:

brew install gnu-sed
like image 83
janos Avatar answered Nov 15 '22 08:11

janos