Deleting comments from c-file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting comments from c-file
# 1  
Old 11-07-2011
Deleting comments from c-file

hii all,
i am writing a shell script to remove comments from a .c/.cpp file.
i have written script as

Quote:
first="\/\*"
last="\*\/"
sed -i "/$first/,/$last/d" $FILE
sed -i "s/$first.*$last//g" $FILE
sed -i '/^\//d' $FILE # deleting single line comment if start with //
the above script file deletes line between /* and */ also lines starting with //.

but the problems are :
1) i dont want to delete the content between /** and */.

2)sed -i '/^\//d' $FILE ,this command doesnt delete the single line comment if it starts with space or tab. but i want to delete that also.Smilie

please help me out ..
thnx
# 2  
Old 11-07-2011
try with this .. If satisfied with the o/p, then use the -i option in sed
Code:
$ sed 's,/\*\*,,g;s,\*/,,g;s,/\*,,g;s,//,,g' infile.cpp

This User Gave Thanks to jayan_jay For This Post:
# 3  
Old 11-07-2011
thanx jayan jay !!
but i dont want to delete those single line comments if it comes after some code
eg. :
function(); // calling a function.

i dont want to delete this comment

---------- Post updated at 02:57 PM ---------- Previous update was at 02:27 PM ----------

Quote:
sed 's,/\*\*,,g;s,\*/,,g;s,/\*,,g;s,//,,g' infile.cpp
its also deleting lines with between /** and */
# 4  
Old 11-07-2011
Post some contents of your input file and expected output ..
# 5  
Old 11-07-2011
test.c
Quote:
#include<stdio.h> //header
#include<stdlib.h> //header
/** print function */
print()
{
printf("hello all");
//printf("hiii");
}
/** main function
*/
int main()
{
//in main function
/*
printf("hello all");
*/
print(); // calling print
}
expected output:
Quote:
#include<stdio.h> //header
#include<stdlib.h> //header
/** print function */
print()
{
printf("hello all");
}
/** main function
*/
int main()
{
print(); // calling print
}
# 6  
Old 11-07-2011
based on your input file ..
Code:
$ sed '/^\/\//d;/^\/\*$/,/^\*\//d' infile.cpp

This User Gave Thanks to jayan_jay For This Post:
# 7  
Old 11-07-2011
thank you very much !! ..
this script is working. but if there is any tab or space is there before single line comment "//" or /* its not deleting that.
also its not deleting lines between
/* */ If it is used in same line i.e. /* HELLO */ this line not getting deleted.(i haven't put that case, its my mistake.)
thanks for your kind help !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script to find comments in file

As I stated in a previous thread - I'm a newbie to Unix/Linux and programming. I'm trying to learn the basics on my own using a couple books and the exercises provided inside. I've reached an exercise that has me stumped. I need to write a bash script that will will read in a file and print the... (11 Replies)
Discussion started by: ksmarine1980
11 Replies

2. Shell Programming and Scripting

Deleting comments from fields

Hi I have this sample data set as follows (called file.txt): hostname1:user1:password hostname2:user1:password #comments comments hostname3:user1:passwordI wish to produce a report as follows: hostname1 user1 password hostname2 user1 password hostname3 user1 passwordie remove all... (11 Replies)
Discussion started by: tsu3000
11 Replies

3. Shell Programming and Scripting

Remove comments from file with specific file name extensions

Hello Unix board community, I have to program a shell script, but I am a complete noob so I hope I get some help here. The assignment is as follows: The program removes all comments regardless of formatting or language from files with specific file name extensions (php, css, js, ...).... (3 Replies)
Discussion started by: TheZeusMan
3 Replies

4. Linux

Not able to sort a file based on it name. Need your expert comments.

Hi, I have a files as shown below and I wanted to sort then in following patter based on there names which has "_" in it. I want to sort them according to feild 6th (bold once)value as shown below. Thanks in advance. File names: 20111014_manish_STEP2_Files_number__5979-6968_ ... (5 Replies)
Discussion started by: manishkomar007
5 Replies

5. UNIX for Dummies Questions & Answers

Remove blank lines and comments from text file

Hi, I am using BASH. How can I remove any lines in a text file that are either blank or begin with a # (ie. comments)? Thanks in advance. Mike (3 Replies)
Discussion started by: msb65
3 Replies

6. Shell Programming and Scripting

Sed script, changing all C-comments to C++-comments

I must write a script to change all C++ like comments: // this is a comment to this one /* this is a comment */ How to do it by sed? With file: #include <cstdio> using namespace std; //one // two int main() { printf("Example"); // three }//four the result should be: (2 Replies)
Discussion started by: black_hawk
2 Replies

7. UNIX for Dummies Questions & Answers

Delete Comments in file

How can I delete comments (lines beginning with /* and ending with */) in file? with single command line..My suggestion is to use grep and sed! (4 Replies)
Discussion started by: aadi_uni
4 Replies

8. Shell Programming and Scripting

AWK to skip comments in XML file

Hello, I'm trying to make a shell script to skip comments from an XML file, but with the code below only deletes comments that are in one line. Can you tell me what can be added here? nawk ' { if($0 !~/<!--/) { a=0 } if($0 ~/<!--/ && $0 ~/-->/) {a=1} if($0 ~/<!--/) {a=1} if... (1 Reply)
Discussion started by: majormark
1 Replies

9. UNIX for Advanced & Expert Users

Add Comments to the specifi lines i na file

I have a requirement like below.I need to Comment some lines in a file. File contains following information. { attribute1 attribute2 atrribute3 attribute4 attribute5 attribute6 attribute7 } I have a requirement like some times i need to comment lines 3 to before '}' and some... (1 Reply)
Discussion started by: ukatru
1 Replies

10. Shell Programming and Scripting

removing comments from file

I'm doing manual way to add and remove "#" on etc/services. Is there anyway I can modify the file using awk or sed or any other program. I use vi to modify /etc/services for enabling telnet , the problem is I don't know how to do it automatically in script. production state: #telnet ... (9 Replies)
Discussion started by: skully
9 Replies
Login or Register to Ask a Question