Removing Multiple Line comments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing Multiple Line comments
# 22  
Old 02-21-2011
You didn't say what you wanted to do with nested comments, what should the output of the following be?

Code:
/* This is the outer comment 
    /* And the inner comment */  
  Some more of the outer */Done!

This awk script allows nested comments (output is "Done!"), but no support for escaped comments (eg quoted text or backslash support):

It calls function p with each char and a lookahead char (n)
Code:
awk 'function p(c,n) {
  if(j) return j=0;
  if(c=="/" && n=="*") {m++; return j=1}
  if(m && c=="*" && n=="/") {m--; return j=1}
  if(!m) printf c; }
{ for(i=1;i<=length($0);i++) p(substr($0,i,1), substr($0,i+1,1)); if(!m) printf "\n"}' infile


Last edited by Chubler_XL; 02-22-2011 at 01:12 AM..
This User Gave Thanks to Chubler_XL For This Post:
# 23  
Old 02-22-2011
Thanks binlib.. that single line thing worked...!!

Chubler and Shell_Life...

Thanks a lot for your help... Both your codes looks impressive (and a bit confusing for a beginner like me) but still I managed to understand that... Yet to try them though...

Thanks a lot for your efforts...
# 24  
Old 02-22-2011
i want to remove ^M from a file through script using sed command...
# 25  
Old 02-23-2011
perl

if file is small, you may try below perl:

Code:
local $/;
my $str = <DATA>;
$str=~s/\/\*[^\*]*\*\//\n/smg;
$str=~s/\n\n/\n/;
open OFH,'| sed -e "s/^  *//" -e "s/  +$//" -e "/^$/d"';
select OFH;
print $str;
close OFH;
__DATA__
/*....
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

# 26  
Old 02-23-2011
i need it in shell script....
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