help me out...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help me out...
# 1  
Old 08-21-2008
Lightbulb help me out...

i am ahving following file

Quote:
curr_date=`date` /*current
date
is
so
and
so*/

i want to remove the multiline comment using sed or awk..
i wrote following code for it..
but its a sed file how can i do it in much simpler way??
Quote:
#!/usr/bin/sed -f
/\/\*/!b
:x
/\*\//!{
N
bx
}
s/\/\*.*\*\///
/^$/d
thanks,
vidya...
# 2  
Old 08-21-2008
There is an example on the perlop man page which is simpler:

Code:
# Delete (most) C comments.
$program =~ s {
    /\*     # Match the opening delimiter.
    .*?     # Match a minimal number of characters.
    \*/     # Match the closing delimiter.
} []gsx;

# 3  
Old 08-21-2008
is there any unix command??
i don't wana go for perl.. and in AIX perlop is not available...
# 4  
Old 08-21-2008
Without perl multi-line regex capability I don't think you'll be able to simplify it too much... unless you change RS in awk maybe? I might try that next. Here is an awk solution, but as you can see it's not short:

Code:
awk '
        /\/\*.*\*\// { gsub("/\*.*\*/",""); print; next }
        /\/\*/ { gsub("\/\*.*",""); print; incomment=1; next }
        incomment && /\*\// { gsub(".*\*\/",""); incomment=0; print }
        incomment { next }
'

It does not work for situations where there are multiple comments on a line, e.g. the following:

Code:
 some code; /* a comment */ some more code; /*another comment */

would become:

Code:
 some code;

# 5  
Old 08-21-2008
This works better:

Code:
awk -v RS='^Y' ' { gsub("/\*.*\*/",""); print; }'

However also doesn't handle multiple comments on a line. You could make it safer like this:

Code:
awk -v RS='^Y' ' { gsub("/\*[^*]*[^/]*\*/",""); print; }'

The only situation that does not handle well is a /* comment * like / this */, which I guess would be pretty rare.

(Incidentally, ^Y is just a randomly chosen, rarely used control character, entered using Ctrl-V Ctrl-Y.)

Last edited by Annihilannic; 08-21-2008 at 04:01 AM.. Reason: Control-Y
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question