-2

Using the terminal (in OSX), how do I modify a specific line of a file?

e.g. I have a javascript file and want to comment out line 300 by prepending //

chepner
  • 446,329
  • 63
  • 468
  • 610
Ashley Coolman
  • 10,209
  • 4
  • 56
  • 76
  • Don't put solutions in your question; if you use something substantially different from the accepted answer, add an answer of your own. – chepner Nov 26 '15 at 13:15

1 Answers1

1

The easiest way is to use sed:

sed -i '300s%^%//%' myfilename.js

Some versions of sed (such as the BSD version that ships with Mac OS X), require an explicit argument for -i. If you don't want a backup, use the empty string.

sed -i "" '300s%^%//%' myfilename.js
chepner
  • 446,329
  • 63
  • 468
  • 610
psmears
  • 23,925
  • 4
  • 38
  • 47