Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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!

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

Use the sed delete command

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

If you need to account for leading space characters:

sed -i '/^\s*[@#]/ d' sample.txt
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.