Coder10
Coder10

Reputation: 65

Deleting single line comments in file

How would I be able to delete single line comments starting with /*......*/ and

By anywhere I mean it can be in a line by itself or after some equation for example.

I was thinking something along the lines of :

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

Upvotes: 3

Views: 920

Answers (2)

Arjun Mathew Dan
Arjun Mathew Dan

Reputation: 5298

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

Upvotes: 3

David C. Rankin
David C. Rankin

Reputation: 84561

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

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

Note: allowable separator replacement varies slightly by OS.

Upvotes: 2

Related Questions