Shell / sed Program To Remove All C and C++ Comments From Program File

in Categories Academic, Script last updated May 6, 2008

Write the shell / sed program which remove all the comments from a simple C program stored in your current directory. You can assume that the c source code contains only syntactically correct comments:
+ start with // and end with a newline

+ starting with /* and ending */ (can be multiline)

+ nesting of comments is not allowed

Make sure that C source is not changed at all.

How do I use this sed script?

$ ./script.sed < input.c $ ./script.sed < input.c > output.c
$ for c in *.c; do script.sed < $c > /tmp/zyzcc.c; /bin/cp -f /tmp/zyzcc.c $c; done

Sample sed code to remove all comments from sed

#! /bin/sed -nf
 
# Remove C and C++ comments, by Brian Hiles (brian_hiles@rocketmail.com)
 
# Sped up (and bugfixed to some extent) by Paolo Bonzini (bonzini@gnu.org)
# Works its way through the line, copying to hold space the text up to the
# first special character (/, ", ').  The original version went exactly a
# character at a time, hence the greater speed of this one.  But the concept
# and especially the trick of building the line in hold space are entirely
# merit of Brian.
 
:loop
 
# This line is sufficient to remove C++ comments!
/^\/\// s,.*,,
 
/^$/{
  x
  p
  n
  b loop
}
/^"/{
  :double
  /^$/{
    x
    p
    n
    /^"/b break
    b double
  }
 
  H
  x
  s,\n\(.[^\"]*\).*,\1,
  x
  s,.[^\"]*,,
 
  /^"/b break
  /^\\/{
    H
    x
    s,\n\(.\).*,\1,
    x
    s/.//
  }
  b double
}
 
/^'/{
  :single
  /^$/{
    x
    p
    n
    /^'/b break
    b single
  }
  H
  x
  s,\n\(.[^\']*\).*,\1,
  x
  s,.[^\']*,,
 
  /^'/b break
  /^\\/{
    H
    x
    s,\n\(.\).*,\1,
    x
    s/.//
  }
  b single
}
 
/^\/\*/{
  s/.//
  :ccom
  s,^.[^*]*,,
  /^$/ n
  /^\*\//{
    s/..//
    b loop
  }
  b ccom
}
 
:break
H
x
s,\n\(.[^"'/]*\).*,\1,
x
s/.[^"'/]*//
b loop

Please note that this program is copied from public domain.

Shell script to read a character (upper or lower), digit, special symbol and display message according to the character entered

in Categories Decision Making last updated April 5, 2008

This script uses sed command to find out all digits or upper characters. -z string (conditional expression) will return true if the length of string is zero.

Script logic

Read one character
[1] Use sed command to determine input character is digit or not (sed -e ‘s/[A-Z]//g’))

[2] Attempt to match regex [0-9] i.e. all digit against the pattern space

[3] If successful, replace that portion matched with replacement // i.e. do nothing.
If digits are in input, it will replace all those digit, and if input is only digit, nothing is left i.e. string is zero and that is what tasted with -z conditional expression.

#!/bin/bash
# Shell script to read a character (upper or lower), digit, special symbol and
# display message according to the character entered
# -----------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
char=""
echo -n "Enter a one character : "
read char
# use sed command to determine input character is digit or not
# -e : start script
# s/[0-9]// : Attempt to match regex [0-9] i.e. all digit against the pattern space (echo $char).
# If  successful,  replace  that  portion  matched  with replacement i.e. // do nothing. 
# g : Global replacement
# so if digit are in input it will replace all those digit, and if input is only digit, nothing
# is left i.e. string is zero and that is what tasted with -z
if [ -z $(echo $char | sed -e 's/[0-9]//g') ]
then
	echo "$char is Number/digit"
elif [ -z $(echo $char | sed  -e 's/[A-Z]//g') ] # find out if character is upper
then
        echo "$char is UPPER character"
 
elif [ -z $(echo $char | sed -e 's/[a-z]//g') ] # find out if character is lower
then
        echo "$char is lower character"
else
	echo "$char is Special symbol" # else it is special character
fi