0

I have a text file text0.txt like

# text0.txt
# This is comment
Hello, I like banana.  # more comments
bra bra bra...

The question is what is the command

$ some_command text0.txt

which outputs

Hello, I like banana.  
bra bra bra...

to standard output.

I'm using bash. Thanks.

ywat
  • 2,757
  • 5
  • 24
  • 32

2 Answers2

2

You want to delete lines that start with a # and to remove a # and any following characters anywhere else on a line. That description practically defines what the sed script should look like:

sed -e '/^#/d' -e 's/[[:space:]]*#.*//' text0.txt

This removes any spaces before the # at the end of a line too. You could combine that into one argument if you insist:

sed '/^#/d; s/[[:space:]]*#.*//' text0.txt

Personally, I think it's easier to read the separate operations when they're in separate arguments, but that's a choice you can make for yourself.

Note that there is no need to run two (or more) commands; one is sufficient.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

with awk:

awk '{a=length($0);gsub("#.*","",$0); if(a==0 || length($0)>0)print $0}' filename
tso
  • 4,732
  • 2
  • 22
  • 32