Text Processing with Sed – Deleting Commented Lines in a Text File

sedtext processing

I am a sed novice. I would like to use sed to delete commented lines in a text file.

In my series of text files, commented lines begin with either the # symbol or the @ symbol (i.e., they are files originally intended for plotting in xmgrace). Suppose I have the following sample file (called sample.txt):

# Comment 1
# Another comment
# Yet another comment
@ More comments
@ Still more comments
data1
data2

data3

I would like to use sed to modify sample.txt, in place, to obtain the following:

data1
data2

data3

where there is a blank line between "data2" and "data3" in both versions of sample.txt.

With inspiration from this Stack Overflow question, I came up with this series of (consecutive) commands:

sed -i 's/#.*$//' sample.txt
sed -i 's/@.*$//' sample.txt

which give this resulting file:

    <blank line>
    <blank line>
    <blank line>
    <blank line>
    <blank line>
data1
data2

data3

where <blank line> is just a blank line. (I had to write it this way so that Stack Exchange would format the output correctly.)

The problem with the above is that the commented lines are only replaced with blank lines, not deleted altogether.

Another possibility of (consecutive) commands is:

sed -i 's/#.*$//' sample.txt
sed -i 's/@.*$//' sample.txt
sed -i '/^$/d' sample.txt

from which I obtain the following output:

data1
data2
data3

The above is, however, also wrong because the blank line between "data2" and "data3" has not been preserved (I am incorrectly deleting all blank lines, indiscriminately). How do I just delete the commented lines?

I am running Ubuntu Linux. Thanks for your time!

Best Answer

Use the sed delete command (here assuming GNU sed as found on Ubuntu and other GNU systems).

 sed -i '/^[@#]/ d' sample.txt

If you need to account for leading space characters:

sed -i '/^\s*[@#]/ d' sample.txt
Related Question