-1

I have this small script to visualize config files without the comment lines:

cat $1 | grep -Ev '[:blank]*#|^[:blank]*$' | pygmentize -g

but it still shows some comments in some files when they have the /* */.

How can I visualize a file, removing all comments, including /* */ ? I'm thinking in making a bash script with a line by line for, searching for the opening and closing of these comments. But I imagine that must be a better way to do this.

All the options I've seen (like this one: http://bit.ly/1BsdcDI) doesn't treat these kind of comments.

Is there an already made tool or script for this ?

Any help apreciated, thanks.

EDIT

as mentioned above, I'm targetting config files specifically. So the rules of exclusion will be:

  • any line starting with '#', ';' or '//'
  • any lines from the start /* to the */
  • don't remove any lines that don't have /* at the start of the line (i.e. have valid codes before /*) or have */ before the end of line (i.e. have valid codes after */)

SOLUTION:

Based in the answer by @cusco and the code by @EdMorton I have created this bash script to do what I want.

#!/bin/bash
[ $# -eq 2 ] && arg="$1" || arg=""
eval file="\$$#"
sed 's/a/aA/g;s/__/aB/g;s/#/aC/g' "$file" |
          gcc -P -E $arg - |
          sed 's/aC/#/g;s/aB/__/g;s/aA/a/g' |
          grep -Ev '^[:blank]*#|^[:blank]*$' |
          grep -Ev '^[:blank]*\;|^[:blank]*$' |
          pygmentize -g
Nelson Teixeira
  • 225
  • 1
  • 3
  • 15
  • 3
    Unless you can be more precise about which **language** the file is in, I don't think this can be done - not least because one chap's comment is another chap's code (think `#include`). – MadHatter Jun 19 '15 at 12:19
  • I complemented the question to clarify. Thanks for pointing this. – Nelson Teixeira Jun 19 '15 at 12:25

1 Answers1

1

check https://stackoverflow.com/questions/13061785/remove-multi-line-comments

it has the following script

[ $# -eq 2 ] && arg="$1" || arg=""
eval file="\$$#"
sed 's/a/aA/g;s/__/aB/g;s/#/aC/g' "$file" |
          gcc -P -E $arg - |
          sed 's/aC/#/g;s/aB/__/g;s/aA/a/g'
cusco
  • 168
  • 1
  • 1
  • 8