tags:

views:

90

answers:

3

hi

how to remove comment lines (as # bal bla ) and empty lines (lines without charecters) from file with one sed command?

THX lidia

+1  A: 

If you're worried about starting two sed processes in a pipeline instead of one (and you probably shouldn't be, it's not that inefficient), you can use multiple -e arguments, something like:

sed -e 's/#.*$//' -e '/^$/d' inputFile

In response to your comment:

but if I want to use the sed -i ( how to integrated )

You can still do that as per the following transcript:

pax> echo 'Line # with a comment' >qq
pax> echo '# Line with only a comment' >>qq
pax> echo >>qq
pax> cat qq
Line # with a comment
# Line with only a comment

pax> sed -i -e 's/#.*$//' -e '/^$/d' qq
pax> cat qq
Line
pax> _

Notice how the file is modified in-place even with two -e options. You can see that both commands are executed on each line.

paxdiablo
but if I want to use the sed -i ( how to integrated )
lidia
That still works just fine, see the update.
paxdiablo
A: 

Alternative variant, using grep:


cat file.txt | grep -Ev '(#.*$)|(^$)'
demetrios
but if the "#" not in begging of the file? ( but line starts with "#")
lidia
cat file.txt | grep -Ev '(^#.*$)|(^$)'to remove lines starting with "#"
demetrios
A: 

you can use awk

awk 'NF{gsub(/^[ \t]*#/,"");print}' file
ghostdog74