제가 잊어버릴까봐 공유합니다.
사용자 스크립트로 먼저 코멘트를 strip 하고
그 결과를 가지고 비교하여 diff 여부를 리턴합니다.
#!/bin/sh
- C, C++ comments aware diff
- this uses stripcomment script to strip comments
Usage=“Usage: {script name} {file1} {file2}”
if [ $# -eq 0 ]; then # if no argument specified echo $Usage # print Usage string value exit 1
fi
if [ $# -eq 1 ]; then # if no argument specified echo $Usage # print Usage string value exit 1
fi
FILE1=$1
FILE2=$2
TMPFILE1=/tmp/aa$$
TMPFILE2=/tmp/bb$$~/bin/stripcomment $FILE1 > $TMPFILE1
~/bin/stripcomment $FILE2 > $TMPFILE2#echo diff $TMPFILE1 $TMPFILE2
diff $TMPFILE1 $TMPFILE2
exit $?
위 스크립트는 제가 짠 거고
아래는 http://bash.cyberciti.biz/academic/sed-remove-c-cpp-comments/ 여기서 퍼왔습니다.
#! /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