hi
how to remove comment lines (as # bal bla ) and empty lines (lines without charecters) from file with one sed command?
THX lidia
hi
how to remove comment lines (as # bal bla ) and empty lines (lines without charecters) from file with one sed command?
THX lidia
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.
Alternative variant, using grep:
cat file.txt | grep -Ev '(#.*$)|(^$)'