Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all lines beginning with a # from a file

Tags:

bash

sed

All of the lines with comments in a file begin with #. How can I delete all of the lines (and only those lines) which begin with #? Other lines containing #, but not at the beginning of the line should be ignored.

like image 746
Village Avatar asked Nov 21 '11 01:11

Village


People also ask

How do I delete all lines?

If you're at the end of a line and want to select the complete line, use the shortcut key combination Shift + Home . If there's only one line of text in the document, or you want to delete all text, press Ctrl + A key to select all text. Once highlighted, press delete to delete everything.

How do you delete certain lines in Word?

Delete lines or connectorsClick the line, connector, or shape that you want to delete, and then press Delete. Tip: If you want to delete multiple lines or connectors, select the first line, press and hold Ctrl while you select the other lines, and then press Delete.

How do I delete a line in Linux?

First, bring your cursor to the line you want to delete. Press the “Esc” key to change the mode. Now type, “:d”, and press “Enter” to delete the line or quickly press “dd”.


1 Answers

This can be done with a sed one-liner:

sed '/^#/d' 

This says, "find all lines that start with # and delete them, leaving everything else."

like image 88
Raymond Hettinger Avatar answered Sep 27 '22 20:09

Raymond Hettinger