How to remove /*...*/ ?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to remove /*...*/ ?
# 1  
Old 03-19-2019
How to remove /*...*/ ?

Sample code from apple's xnu code:

Code:
struct radix_node {
	struct	radix_mask *rn_mklist;	/* list of masks contained in subtree */
	struct	radix_node *rn_parent;	/* parent */
	short	rn_bit;			/* bit offset; -1-index(netmask) */
	char	rn_bmask;		/* node: mask for bit test*/
	u_char	rn_flags;		/* enumerated next */
      ...
}

I want to remove only all inline comments with pattern /*...*/ but not multi line comments

The result would be

Code:
struct radix_node {
	struct	radix_mask *rn_mklist;
	struct	radix_node *rn_parent;
	short	rn_bit;
	char	rn_bmask;
	u_char	rn_flags;
      ...
}

I knew using sed will work but I am new with it.
# 2  
Old 03-19-2019
something to start with and improve:
Code:
sed 's#/\*.*\*/##g' myFIle

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 03-19-2019
Quote:
Originally Posted by cmdcmd
I want to remove only all inline comments with pattern /*...*/ but not multi line comments
[...]
I knew using sed will work but I am new with it.
You are right, sed is the correct tool for virtually absolutely everything, including what you want to do. ;-))

Here is the naive approach, which will work on your example:

Code:
sed 's/\/\*.*\*\///' /path/to/file > /out/put/file

The (conceivable) problem with this approach is that regexps are "greedy", which means that always the longest-possible match is selected. That means: the above regexp selects "/*", followed by any number of any characters, followed by "*/". In case you have several comments in one line like this:

Code:
some_code; /* comment */   some_more; /* other comment */

The regexp would match the text marked in bold:

Code:
some_code; /* comment */   some_more; /* other comment */

In case you need to take care of this (perhaps rather rare) possibility you will have to refine the way you match the characters in between the introductory comment sequence and the comment-end-sequence. For instance:

Code:
sed 's/\/\*[^/*]*\*\///g' /path/to/file > /out/put/file

This will match "/*", followed by any number of any characters save for "/" or "*", followed by a "*/". This is better and will work on the previous example but will be fooled if a single "/" or "*" appears in the comments like this input:

Code:
code; /* comment * comment */
code; /* comment / comment */

You can put more work into it and refine the regexp even further but ultimately there will be (in a strict sense) no solution to your problem because a regexp engine (like sed is in the core) cannot replace a parser. See here for a more thorough discussion of this subject.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 4  
Old 03-19-2019
This is a case for a mimimum match. With perl:
Code:
cat test
code; /* comment * comment */
code; /* comment / comment */ /* another comment */
code; /* a comment with a / and a * */

Code:
perl -pe 's#/\*.*?\*/##g' test
code; 
code;  
code;

The *? is the minimum match, as opposed to the greedy *.
/\*.*?\*/ is a minimum amount of (. = any) characters between /* and */.

--- Post updated at 21:43 ---

The following should even eliminate /* ... */ that span over multiple lines:
Code:
perl -pe 'if (defined $nop) { if (s#.*?\*/##) { undef $nop; } else { $_="" } } s#/\*.*?\*/##g; $nop=1 if s#/\*.*##;'

# 5  
Old 03-19-2019
Quote:
Originally Posted by MadeInGermany
This is a case for a mimimum match.
You are right: non-greedy matching takes care of a few cases. Still, what i said about regexps being unable to parse still stands. For instance, your perl-program would have problems with:

Code:
char foo[3]="/*"; char bar[3]="*/"; /* comment */

I admit, this (and some more cases i could cite) are fringe. Maybe the thread-O/P will never encounter any of these. But again, it is - in a strict sense - impossible to overcome all of these problems (some of them, yes, but not all) because a regexp engine cannot work as a parser.

I hope this helps.

bakunin
These 2 Users Gave Thanks to bakunin For This Post:
# 6  
Old 03-19-2019
Thank you all for good working solutions. That what I want. Very appreciate your help.

cmdcmd Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash to remove find and remove specific extension

The bash below executes and does find all the .bam files in each R_2019 folder. However set -x shows that the .bam extension only gets removed from one .bam file in each folder (appears to be the last in each). Why is it not removing the extension from each (this is $SAMPLE)? Thank you :). set... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Post Here to Contact Site Administrators and Moderators

Please remove this post/remove information from it

In this thread: /shell-programming-and-scripting/255687-organizing-text-file-capital-names-capital-word-capital-word.html (sorry i cant use links) that is not an example, those are real students names with real student login id's for the college i am attending and i am on that list. Please... (3 Replies)
Discussion started by: throwawayacc
3 Replies

3. AIX

Need to remove (LP)PP

Had increased FS system size (sample_lv) on particular disks hdisk189 hdisk190 in a shared FS but unfortunately given addnl size occupies the space on other disks hdisk78 hdisk40 too In case, need to remove the addnl lv size occupied on hdisk78 hdisk40. How to achieve it. Pls advice. ... (3 Replies)
Discussion started by: ksgnathan
3 Replies

4. Red Hat

Remove

Hi guys, I need to write a script so that when i execute the "rm" command, the file mentioned need to be copied to other folder and then be deleted. this should be done in back ground. can you please help me out?? (1 Reply)
Discussion started by: rajeshb6
1 Replies

5. Shell Programming and Scripting

remove brackets and put it in a column and remove repeated entry

Hi all, I want to remove the remove bracket sign ( ) and put in the separate column I also want to remove the repeated entry like in first row in below input (PA156) is repeated ESR1 (PA156) leflunomide (PA450192) (PA156) leflunomide (PA450192) CHST3 (PA26503) docetaxel... (2 Replies)
Discussion started by: manigrover
2 Replies

6. Shell Programming and Scripting

How to remove ^I ?

not sure what is this but any can help me delete this ^I cat -A file.txt CLAS^I^I|890^I|7,10,12,341,305,308,29,54^M$ LCLS^I^I|891^I|7,10,12,341,305,308,29,54^M$ MURB^I^I|892^I|7,10,12,341,305,308,29,54^M$ LATI^I^I|893^I|7,10,12,341,305,308,29,54^M$ i want to remove the ^I^I... (2 Replies)
Discussion started by: posner
2 Replies

7. Shell Programming and Scripting

remove with [0-9]

My file has varied width references: width=10% style=width:5% width:1506% width:99.58% so I'm trying clear all the width calls with one procedure: 's/width= *%//' and 's/width=*%//'but neither is working. (6 Replies)
Discussion started by: dba_frog
6 Replies

8. Shell Programming and Scripting

remove me!

Clicked on post twice, sorry: https://www.unix.com/shell-programming-scripting/147954-grep-fixed-string-regex.html#post302469753 (1 Reply)
Discussion started by: teresaejunior
1 Replies

9. UNIX for Dummies Questions & Answers

remove except ...

hello, Sometimes I need to remove all the files except one or more.I mean, there are 90 files and I want to remove 88 of them. how can i do that?is it possible to tell the "rm" command not to remove specified files? (4 Replies)
Discussion started by: xyzt
4 Replies

10. UNIX for Dummies Questions & Answers

smitty, remove user, remove directory as well..

hi, i am on aix. i used smitty to remove a user.. but then found that its directory still exists.... so i have to remove the directory manually... am i doing it the right way? (2 Replies)
Discussion started by: yls177
2 Replies
Login or Register to Ask a Question