≡ Menu

Shell / Sed Script To Remove All Comments From C Program

Run this script as follows:
cat file.c | ./script.sh
sample file.c:

Sample output:

Sed Script


#!/bin/sed -f
# Simple Sed Program to remove all comments from c program
# --------------------------------------------------------------------
# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2005 nixCraft project.
# Feedback/comment/suggestions : http://cyberciti.biz/fb/
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
# if no /* get next
/\/\*/!b
# here we've got an /*, append lines until get the corresponding
# */
:x
/\*\//!{
N
bx
}
# delete /*...*/
s/\/\*.*\*\///

{ 1 comment… add one }
  • vlp August 18, 2010, 11:53 am

    Does not work for me with the following:
    +++/*—-*/+++/*—-
    –*/+++

    (should give +++++++++)

Security: Are you a robot or human?

Leave a Comment