5

Short questing, why does this line

sed -i '1s/^/#!\/usr\/bin\/env node\n/' tsunit.js;\

Give me this error

sed: 1: "tsunit.js": undefined label 'sunit.js'

in a Makefile, if relevant.

I’m on a Mac.

SomeNorwegianGuy
  • 1,494
  • 4
  • 17
  • 43
  • you can use another separator in `s` if there is already `/(slash)`, like `sed 's@/foo/bar/blah@replacement@g' file ` save a lot of escape and make it readable. – Kent Apr 14 '14 at 23:01
  • possible duplicate of [Variations of sed between OSX and GNU/Linux](http://stackoverflow.com/questions/2320564/variations-of-sed-between-osx-and-gnu-linux) – that other guy Apr 14 '14 at 23:13

2 Answers2

9

According to the Apple man page for sed, the -i option takes a required argument specifying the file extension for the backup file. As a result, assuming that you are on a Mac or similar, sed believes that you intended '1s/^/#!\/usr\/bin\/env node\n/' to be the file extension of the backup. It then interprets tsunit.js as a sed command. the leading t tells sed to branch to the label sunit.js which, of course, doesn't exist. Hence the error message.

The solution is:

sed -i '.bak' '1s/^/#!\/usr\/bin\/env node\n/' tsunit.js

Or, if you really do not want a backup:

sed -i '' '1s/^/#!\/usr\/bin\/env node\n/' tsunit.js
John1024
  • 109,961
  • 14
  • 137
  • 171
1

Also, it looks like you're inserting a line. sed has more commands than s

sed -i "" '1i\
#!/usr/bin/env node' tsunit.js
glenn jackman
  • 238,783
  • 38
  • 220
  • 352