-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
  • 497,756
  • 71
  • 530
  • 681
Ashley Coolman
  • 11,095
  • 5
  • 59
  • 81
  • 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
  • 497,756
  • 71
  • 530
  • 681
psmears
  • 26,070
  • 4
  • 40
  • 48