Wednesday, June 20, 2007

Strip bash comments with grep

Sometimes, you just want to see the code ... Let's say you want to look at a bash script, but strip out all the comments. Here's a sample command using grep that will display a bash script, without comments, to stout:
grep -v "^#" commented-script.sh
Notes:
  • "-v" means invert the selection; i.e., only display the lines in the file that don't match.
  • "^#" is the regular expression that matches "lines that start with #".
  • "^" means only match the beginning of the line.
  • Replace "commented-script.sh" with the file name of the file you want to strip.
Tips:
  • This command strips single line comments from any programming language. Just change the comment line character (";" instead of "#" for example).
  • If you just want to read the comments, drop the "-v" switch.

0 comments: