How to prevent sed command removing whole line?

hello,

I'm using sed command to extract the text between 2 special characters which are /* and */
I used following command to do this.

sed -n '/\/\*/,/\*\//p' file.txt

But if the file.txt contains a line something like this,

a=5;    /* this is a comment */

the above command extracts this whole line whereas I want only /* this is a comment */ part.

How to overcome this?

try this

echo "a=5; /* this is a comment */" | sed "s;.*/\*\(.*\)\*/;\1;"
1 Like

Slight modification of itkamaraj's fine proposal to yield the full required output:

echo "a=5; /* this is a comment */" | sed "s;.*\(/\*.*\*/\);\1;"
/* this is a comment */
2 Likes

Hi beginner_99,
Note that the suggestions you have received work for your sample input (and any other input that has no comments or exactly one complete comment on each line). The suggestions will not work with the following "code":

/*
** Multi-line comment.
*/
/* Multiple comments... */ code; /* ... on one line. */

Are either of these limitations a problem for the real data you will be processing?