Delete all lines beginning with a # from a file

BashSed

Bash Problem Overview


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.

Bash Solutions


Solution 1 - Bash

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."

Solution 2 - Bash

I'm a little surprised nobody has suggested the most obvious solution:

grep -v '^#' filename

This solves the problem as stated.

But note that a common convention is for everything from a # to the end of a line to be treated as a comment:

sed 's/#.*$//' filename

though that treats, for example, a # character within a string literal as the beginning of a comment (which may or may not be relevant for your case) (and it leaves empty lines).

A line starting with arbitrary whitespace followed by # might also be treated as a comment:

grep -v '^ *#' filename

if whitespace is only spaces, or

grep -v '^[  ]#' filename

where the two spaces are actually a space followed by a literal tab character (type "control-v tab").

For all these commands, omit the filename argument to read from standard input (e.g., as part of a pipe).

Solution 3 - Bash

The opposite of Raymond's solution:

sed -n '/^#/!p'

"don't print anything, except for lines that DON'T start with #"

Solution 4 - Bash

you can directly edit your file with

sed -i '/^#/ d'

If you want also delete comment lines that start with some whitespace use

sed -i '/^\s*#/ d'

Usually, you want to keep the first line of your script, if it is a sha-bang, so sed should not delete lines starting with #!. also it should delete lines, that just contain only a hash but no text. put it all together:

sed -i '/^\s*\(#[^!].*\|#$\)/d'

To be conform with all sed variants you need to add a backup extension to the -i option:

sed -i.bak '/^\s*#/ d' $file
rm -Rf $file.bak

Solution 5 - Bash

You can use the following for an awk solution -

awk '/^#/ {sub(/#.*/,"");getline;}1' inputfile

Solution 6 - Bash

This answer builds upon the earlier answer by Keith.

egrep -v "^[[:blank:]]*#" should filter out comment lines.

egrep -v "^[[:blank:]]*(#|$)" should filter out both comments and empty lines, as is frequently useful.

For information about [:blank:] and other character classes, refer to https://en.wikipedia.org/wiki/Regular_expression#Character_classes.

Solution 7 - Bash

You also might want to remove empty lines as well

sed -E '/(^$|^#)/d' inputfile

Solution 8 - Bash

Here is it with a loop for all files with some extension:

ll -ltr *.filename_extension > list.lst

for i in $(cat list.lst | awk '{ print $8 }') # validate if it is the 8 column on ls 
do
    echo $i
    sed -i '/^#/d' $i
done

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVillageView Question on Stackoverflow
Solution 1 - BashRaymond HettingerView Answer on Stackoverflow
Solution 2 - BashKeith ThompsonView Answer on Stackoverflow
Solution 3 - BashataView Answer on Stackoverflow
Solution 4 - Bashrubo77View Answer on Stackoverflow
Solution 5 - Bashjaypal singhView Answer on Stackoverflow
Solution 6 - BashAsclepiusView Answer on Stackoverflow
Solution 7 - BashAhmed KhaledView Answer on Stackoverflow
Solution 8 - BashBruno DamasView Answer on Stackoverflow