Removing Multiple Line comments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing Multiple Line comments
# 1  
Old 02-18-2011
Removing Multiple Line comments

Smilie Hi all,

I would like to remove all multiple line comments, in between the pattern /*.....*/ from my file.

The comments can be expected to be at any place in my file, like this:
Code:
/*....
This multi line comment might stretch even the entire file..!
......*/
Some text
Text1 /* Comment here */
/*...Comment...*/ Text2
Some text
Note this text /*.....
....
......*/ Note here again


I would like my output to be like this:
Code:
Some text
Text1
Text2
Some text
Note this text
Note here again


I tried expressions like

Code:
sed -e 's~/\*[^$]*\*/~~' -e '/\/\*/,/\*\//d'
sed 's/\/\*[^*]*\*\///g'

and many such variations of sed.

I did try searching the forums here but one issue still remains...

This part of the file
Code:
Note this text /*.....
....
......*/ Note here again

is getting deleted completely along with the proper text Note this text and Note here again.

Please help me out on this... Smilie

Last edited by Franklin52; 02-18-2011 at 04:16 AM.. Reason: Please use code tags, thank you
# 2  
Old 02-18-2011
This may be a start:
Code:
sed 's|/\*|\n&|g;s|*/|&\n|g' infile | sed '/\/\*/,/*\//d'

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 02-18-2011
Try:
Code:
perl -p0e 's!/\*.*?\*/!!sg' file

This User Gave Thanks to bartus11 For This Post:
# 4  
Old 02-18-2011
Thank you Scrutinizer...

The main issue of this part...

Code here; /*..... comment starts
Code here; comment ends */

This comment section is not removed...

I get it as

Code here;
Code here; comment ends *


Pls help on this


---------- Post updated at 01:37 PM ---------- Previous update was at 01:25 PM ----------

Thanks Bartus... but still the issue remains as replied to Scrutinizer... Smilie

Last edited by Jackuar; 02-18-2011 at 04:04 AM..
# 5  
Old 02-18-2011
Try:
Code:
sed 's|/\*|\n&|g;s|*/|&\n|g' infile | sed -n '/\/\*/,/*\//{H;$!d;};p;$g;$woutfile'

The comments should be in "outfile"
# 6  
Old 02-18-2011
@Scrutinizer..

I get a blank file as output... !! As if everything is deleted...!
# 7  
Old 02-18-2011
What OS are you using?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing carriage returns from multiple lines in multiple files of different number of columns

Hello Gurus, I have a multiple pipe separated files which have records going over multiple Lines. End of line separator is \n and records going over multiple lines have <CR> as separator. below is example from one file. 1|ABC DEF|100|10 2|PQ RS T|200|20 3| UVWXYZ|300|30 4| GHIJKL|400|40... (7 Replies)
Discussion started by: dJHa
7 Replies

2. Shell Programming and Scripting

How to ignore comments at the end of the each line?

Hi All, I am reading the host file by ignoring the comments and write it to the other file. I am reading with regular expression for IP address. grep -E '^{1,3}\.{1,3}\.{1,3}\.{1,3}' $inputFile | awk '{for(i=2;i<=NF;i++)print $1,$i}' > $DR_HOME/OS/temp After that am reading each host... (4 Replies)
Discussion started by: sharsour
4 Replies

3. UNIX for Dummies Questions & Answers

Remove multi line and single line comments

Hi, I am trying to remove multi line and single line comments like examples below I have tried this pattern. it works fine for single line comments and multi line comments in a single line only. but this fails when the comments are extended in multiple lines as shown in the comment 2 of... (3 Replies)
Discussion started by: ahmedwaseem2000
3 Replies

4. Shell Programming and Scripting

Removing SAS multi line comments in UNIX

i have to remove the commented (/* . . . .*/) part which starts in one line and ends in other.help me with generic code because i have 1000 to 10k lines code which i have to remove. data one; set work.temp; input name age; infile filename; /* dfsdf dsfs sdfdf dsdd sdfsf sdfsf sfs... (4 Replies)
Discussion started by: saaisiva
4 Replies

5. Shell Programming and Scripting

Removing carriage return/line feeds on multiple lines

I would like to remove carriage returns/line feeds in a text file, but in a specific cadence: Read first line (Header Line 1), remove cr/lf at the end (replace it with a space ideally); Read the next line (Line of Text 2), leave the cr/lf intact; Read the next line, remove the cr/lf; Read... (14 Replies)
Discussion started by: tomr2012
14 Replies

6. Shell Programming and Scripting

How to filter only comments while reading a file including line break characters.

How do I filter only comments and still keep Line breaks at the end of the line!? This is one of the common tasks we all do,, How can we do this in a right way..!? I try to ignore empty lines and commented lines using following approach. test.sh # \040 --> SPACE character octal... (17 Replies)
Discussion started by: kchinnam
17 Replies

7. 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

8. Shell Programming and Scripting

Removing end of line to merge multiple lines

I'm sure this will be an easy question for you experts out there, but I have been searching the forum and working on this for a couple hours now and can't get it right. I have a very messy data file that I am trying to tidy up - one of the issues is some records are split into multiple lines: ... (4 Replies)
Discussion started by: tink
4 Replies

9. 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

10. Solaris

Removing user from multiple groups via command line

Want to know if any, a command line parameter(s) of removing a user from multiple groups without using any ineractive application? (1 Reply)
Discussion started by: jquizon62
1 Replies
Login or Register to Ask a Question