Removing single line comments in a file

How do I manage to remove single comments starting with /*......*/

and

Anyway, I mean it could be on a separate line or after some equation, for example.

I was thinking something like:

s/\/\*.*\*\///g'

      

+3


source to share


2 answers


sed -e 's|/\*.*\*/||g'                 => to remove /* ... */
sed -e 's|//.*||g'                     => to remove //...
sed -e 's|/\*.*\*/||g' -e 's|//.*||g'  => to remove both /* ... */ and //...

      

Example:



sdlcb@ubuntu:~$ cat file
jksdjskjdsd /* jdskdskd */
jdskdjsd // ksldksldsdks
uiiu
sdlcb@ubuntu:~$ sed -e 's|/\*.*\*/||g' -e 's|//.*||g' file
jksdjskjdsd 
jdskdjsd 
uiiu

      

+1


source


Or, if you don't like the look of the picket, you can change the separator and also use character classes to avoid having to avoid everything:

's|/[*].*[*]/||g'

      



Note: The valid separator replacement varies slightly on OS.

+2


source







All Articles