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 Searches For A Single Word Pattern Recursively

in Categories Academic last updated May 5, 2008
#!/bin/bash
# Write a shell script that searches for a single word pattern recursively
# in the current working directory and display the numbers of times it occurred.
# -------------------------------------------------------------------------
# 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.
# ----------------------------------------------------------------------
echo -n "Enter one word pattern : "
read pattern
# wc  - word count
COUNT=$(grep -wr "${pattern}" . 2> /dev/null | wc -l)
echo "${pattern} occurred ${COUNT} times."

Shell Script To Display Last Five Lines Of The File

in Categories Academic last updated April 12, 2008

Given the filename by the user as the input, write a shell script to display the last five lines of the file.

Shell Script

#!/bin/bash
# get filename
echo -n "Enter File Name : "
read fileName
 
# make sure file exits for reading
if [ ! -f $fileName ]; then
  echo "Filename $fileName does not exists"
  exit 1
fi
 
# display last five lines of the file using tail command
tail -5 $fileName

Command Line Argument

You can read file name as command line argument:

#!/bin/bash
# get filename
fileName="$1"
# make sure command line arg provided 
if [ -z $1 ]; then
	echo "Syntax: $(basename $0) filename"
	exit 1
fi
# make sure file exits for reading
if [ ! -f $fileName ]; then
  echo "Filename $fileName does not exists"
  exit 1
fi
# ok display last five lines of the file using tail command
tail -5 $fileName

Shell script to find whether an input number is palindrome or not

in Categories Academic, Shell Math last updated April 12, 2008

A palindrome is a word, phrase, number or other sequence of units that has the property of reading the same in either direction (the adjustment of punctuation and spaces between words is generally permitted). Composing literature in palindromes is an example of constrained writing. The word “palindrome” was coined from Greek roots palin (“back”) and dromos (“way, direction”) by English writer Ben Jonson in the 1600s.

A palindromic number is a number where the digits, with decimal representation usually assumed, are the same read backwards, for example, 58285. They are studied in recreational mathematics where palindromic numbers with special properties are sought. A palindromic prime is a palindromic number that is a prime number.

#!/bin/bash
# Shell script to find whether an input number is palindrome or not
# -----------------------------------------------
# Copyright (c) 2006 nixCraft <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.
# -------------------------------------------------------------------------
 
echo -n "Enter number : "
read n
 
# store single digit
sd=0
 
# store number in reverse order
rev=""
 
# store original number
on=$n
 
while [ $n -gt 0 ]
do
    sd=$(( $n % 10 )) # get Remainder 
    n=$(( $n / 10 ))  # get next digit
    # store previous number and current digit in reverse 
    rev=$( echo ${rev}${sd} ) 
done
 
if [ $on -eq $rev ];
then
  echo "Number is palindrome"
else
  echo "Number is NOT palindrome"
fi

Shell program to read a number and find the sum of digits

in Categories Academic, Shell Math last updated April 8, 2008
#!/bin/bash
# Shell program to read a number and find the sum of digits
# -----------------------------------------------
# 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.
# -------------------------------------------------------------------------
 
echo -n "Enter number : "
read n
 
# store single digit
sd=0
 
# store number of digit 
sum=0
 
# use while loop to caclulate the sum of all digits
while [ $n -gt 0 ]
do
    sd=$(( $n % 10 )) # get Remainder 
    n=$(( $n / 10 ))  # get next digit
    sum=$(( $sum + $sd )) # calculate sum of digit
done
echo  "Sum of all digit  is $sum"

Shell program to read a number and reverse the number

in Categories Academic, Shell Math last updated April 8, 2008
#!/bin/bash
# Shell program to read a number and reverse the number
# for example 123 should output as 321
# -----------------------------------------------
# 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.
# -------------------------------------------------------------------------
 
echo -n "Enter number : "
read n
 
# store single digit
sd=0
 
# store number in reverse order
rev=""
 
# store original number
on=$n
 
# use while loop to caclulate the sum of all digits
while [ $n -gt 0 ]
do
    sd=$(( $n % 10 )) # get Remainder 
    n=$(( $n / 10 ))  # get next digit
    # store previoues number and current digit in rev
    rev=$( echo ${rev}${sd} ) 
done
 
echo  "$on in a reverse order $rev"

Shell program to calculate the number of digits in a number read from the user

in Categories Academic, Shell Math last updated April 8, 2008
#!/bin/bash
# Shell program to calculate the number of digits in a
# number read from the user
# -----------------------------------------------
# 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.
# -------------------------------------------------------------------------
 
echo -n "Enter number : "
read n
 
sd=0
 
# store number of digit 
nd=0
on=$n # store $n so that we can use it later
 
# use while loop to caclulate the number of digit
while [ $n -gt 0 ]
do
    sd=$(( $n % 10 )) # get Remainder 
    n=$(( $n / 10 )) 
    nd=$(( $nd + 1)) # calculate all digit in a number till n is not zero
done
echo  "Numnber of digit in a $on is $nd"

Shell program to read two numbers and display all the odd numbers berween those two number

in Categories Academic, Decision Making last updated April 8, 2008
#!/bin/bash
# Shell program to read two numbers and display all the odd
# numbers berween those two number
# -----------------------------------------------
# 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.
# -------------------------------------------------------------------------
 
echo -n "Enter first number : "
read n1
 
echo -n "Enter second number : "
read n2
 
if [ $n2 -gt $n1 ];
then
  for(( i=$n1; i<=$n2; i++ ))
  do
    # see if it is odd or even number
    test=$(( $i % 2 ))
    if [ $test -ne 0 ]; 
    then
      echo $i
    fi   
  done
else
  echo "$n2 must be greater than $n1, try again..." 
fi

Shell program to display numbers from 1 to 10

in Categories Academic last updated April 8, 2008
#!/bin/bash
# Shell program to display numbers from 1 to 10
# -----------------------------------------------
# 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.
# -------------------------------------------------------------------------
 
# using for loop
echo "Using for loop method # 1... "
for i in 1 2 3 4 5 6 7 8 9 10
do
  echo -n "$i " 
done
echo ""
 
# this is much better as compare to above for loop
echo "Using for loop method # 2... "
for (( i=1; i<=10; i++ ))
do
   echo -n "$i "
done
echo ""
 
# use of while loop
echo "Using while loop..."
j=1
while [ $j -le 10 ]
do
   echo -n "$j "
   j=$(( j + 1 )) # increase number by 1 
done
echo ""