Remove comments like pattern from text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove comments like pattern from text
# 1  
Old 02-29-2016
Question Remove comments like pattern from text

Hi , We need to remove comment like pattern from a code text. The possible comment expressions are as follows.

Input
Code:
BizComment : Special/*@
    Name:bzt_53_3aea640a_51783afa_5d64_0
    BizHidden:true
@*/

/*   lookup Disease 
Category Therapuetic Class   */

a=b;
out0::bzt_53_3aea640a_51783afa_5d64_0(in0)= 1; /* Final assignment */



Output

Code:
BizComment : Special

a=b;
out0::bzt_53_3aea640a_51783afa_5d64_0(in0)= 1;

Basically, requirement is to replace
Code:
/@* ... *@/

and
Code:
/* .... */

style comments.

Also can you please guide the concept of using regex in similar scenarios.

Last edited by vbe; 02-29-2016 at 10:11 AM.. Reason: code tags please... not ICODE
# 2  
Old 02-29-2016
How about
Code:
sed '/\/\*/{:L; s/\*\//&/; tK; N; bL; :K; s/\/\*.*\*\///} ' file
BizComment : Special



a=b;
out0::bzt_53_3aea640a_51783afa_5d64_0(in0)= 1;

# 3  
Old 02-29-2016
Note that if you have source code that prints comments or looks for comments:
Code:
/* printf format string to print comments */
char fmt[]='/*
  %s
*/\n'

you'll need a MUCH more complex parser to decide what is code and what is a comment.
# 4  
Old 02-29-2016
try also gnu awk:
Code:
awk '$1=$1' RS= FS="/[*].*[*]/" infile

# 5  
Old 03-01-2016
To handle this with regexp, I suggest that you use a language, where the regexp engine can do balanced matching - for example Ruby or Perl. See for reference this posting: regex - Matching balanced parenthesis in Ruby using recursive regular expressions like perl - Stack Overflow
# 6  
Old 03-01-2016
Thanks a lot everyone. Your suggestions helped to solve my problem.

I understood the code but not completely. Can you please help me to understand for both awk and sed.
# 7  
Old 03-01-2016
The sed, when finding an comment opener, if need be loops, reading and appending new lines, until it finds the comment closer, then removes the entire comment.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove duplicate occurrences of text pattern

Hi folks! I have a file which contains a 1000 lines. On each line i have multiple occurrences ( 26 to be exact ) of pattern folder#/folder#. # is depicting the line number in the file some text here folder1/folder1 some text here folder1/folder1 some text here folder1/folder1 some text... (7 Replies)
Discussion started by: martinsmith
7 Replies

2. UNIX for Dummies Questions & Answers

Remove SAS comments using UNIX

I have tried a lot, Need your help guys. SAS Program: data one ; /* Data step */ Input name $; /*Dec variables*/ I want to remove the commented part(/* Data step */) alone. I have tried using sed command but it is deleting the entire line itself. i need unix command to separate this and... (1 Reply)
Discussion started by: saaisiva
1 Replies

3. Shell Programming and Scripting

How to remove comments from a bash script?

I would like to remove comments from a bash script. In addition, I would like to remove lines that consist of only white spaces, and to remove blank lines. #!/bin/bash perl -pe 's/ *#.*$//g' $1 | grep -v ^]*$ | perl -pe 's/ +/ /g' > $2 # # $1 INFILE # $2 OUTFILE The above code... (10 Replies)
Discussion started by: LessNux
10 Replies

4. Shell Programming and Scripting

Help with remove last text of a file that have specific pattern

Input file matrix-remodelling_associated_8_ aurora_interacting_1_ L20 von_factor_A_domain_1 ATP_containing_3B_ . . Output file matrix-remodelling_associated_8 aurora_interacting_1 L20 von_factor_A_domain_1 ATP_containing_3B . . (3 Replies)
Discussion started by: perl_beginner
3 Replies

5. Shell Programming and Scripting

sed remove comments

I need to use sed to remove comments from files. I am using this, but it only works on comments that start at the beginning of the line. sed /^"\/\/"/d In most of the files I have comments like this: code // Comments or tab // Comments (5 Replies)
Discussion started by: gravesit
5 Replies

6. Shell Programming and Scripting

sed remove css comments

Is there a way that I can use sed to remove lines with css comments like this? /* comment */ (9 Replies)
Discussion started by: gravesit
9 Replies

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

8. Shell Programming and Scripting

How to remove all text except pattern

i have nasty html file with 2000+ simbols in 1 row...i need to remove whole the code except title="Some title..." and store those into file with titles (the whole text is in variable text) i've tried something like this: echo $text | sed 's/.*\(title=\".+\"\).*/\1/' > titles.html BUT it does... (13 Replies)
Discussion started by: Lukasito
13 Replies

9. Shell Programming and Scripting

how can i remove comments in random positions in a file?(bash)

Suppose i have a file like this: #bla bla #bla bla bla bla bla Bla BLA BLA BLA #bla bla .... .... how can i remove all comments from every line,even if they are behind commands or strngs that are not comments? any idea how i could do that using awk? (2 Replies)
Discussion started by: bashuser2
2 Replies

10. UNIX for Dummies Questions & Answers

Remove comments...

It may be a no-brainer, but the answer is escaping me right now: I'm trying to write a little script to remove all comments from .c source... I was thinking sed, but I'm not a very strong regexp user (e.g. I suck with sed). I tried dumping the file into: sed -e 's/\/\* * \*\///g' and several... (1 Reply)
Discussion started by: LivinFree
1 Replies
Login or Register to Ask a Question