Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
647 views
in Technique[技术] by (71.8m points)

bash - Delete all comments in a file using sed

How would you delete all comments using sed from a file(defined with #) with respect to '#' being in a string?

This helped out a lot except for the string portion.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If # always means comment, and can appear anywhere on a line (like after some code):

sed 's:#.*$::g' <file-name>

If you want to change it in place, add the -i switch:

sed -i 's:#.*$::g' <file-name>

This will delete from any # to the end of the line, ignoring any context. If you use # anywhere where it's not a comment (like in a string), it will delete that too.

If comments can only start at the beginning of a line, do something like this:

sed 's:^#.*$::g' <file-name>

If they may be preceded by whitespace, but nothing else, do:

sed 's:^s*#.*$::g' <file-name>

These two will be a little safer because they likely won't delete valid usage of # in your code, such as in strings.

Edit:

There's not really a nice way of detecting whether something is in a string. I'd use the last two if that would satisfy the constraints of your language.

The problem with detecting whether you're in a string is that regular expressions can't do everything. There are a few problems:

  • Strings can likely span lines
  • A regular expression can't tell the difference between apostrophies and single quotes
  • A regular expression can't match nested quotes (these cases will confuse the regex):

    # "hello there"
    # hello there"
    "# hello there"
    

If double quotes are the only way strings are defined, double quotes will never appear in a comment, and strings cannot span multiple lines, try something like this:

sed 's:#[^"]*$::g' <file-name>

That's a lot of pre-conditions, but if they all hold, you're in business. Otherwise, I'm afraid you're SOL, and you'd be better off writing it in something like Python, where you can do more advanced logic.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...