4

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!

10

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
1
grep '^[^@#]' < file.in > file.out

would get you the lines that start with any character but @ and # so effectively remove the #foo, @foo lines but also the empty lines.

grep -v '^[@#]' < file.in > file.out

would preserve the empty lines. If you want consider lines that have blanks before the @, #:

grep -v '^[[:blank:]]*[@#]' < file.in > file.out

And to also remove blank lines:

grep '^[[:blank:]]*[^[:blank:]@#]' < file.in > file.out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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