0

Have tried to get this to work for a while now without any success. I know there are similar posts about this topic but their examples doesn't work or their regexp's are to violent for my purpouse. This is to heavy: Comprehensive RegExp to remove JavaScript comments

My issue is quite simple, i have a unified javascript file created with a deploy.sh script and would like to have every line removed where the first characters occuring are "//" ignoring any preceding whitespaces and tabs like this:

// Remove this line
var foobar = "sometext";
    // remove this line with tab(s) before comment
    function foobar(); // Do not remove this!
  // remove this line with whitespace(s) before comment

This should be achived with a linux/ubuntu native tool or program language executed inside a shellscript without any external files useing something like sed.

I think having a convention of just using theese conditions for comment lines as removable should be quite straightforward with a minimal risk of misinterpreting. If i then like to have some comments to show in the production version i can then use the multiline comment annotation /* Comment */

Community
  • 1
  • 1
Dominus
  • 67
  • 5

3 Answers3

1

Just tell grep to discard everything starting with zero or more space sequences followed by //:

$ grep -v "^\s*//" file
var foobar = "sometext";
    function foobar(); // Do not remove this!
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 1
    This worked, had to set the output to a temporary file first like this: grep -v "^\s*//" unified.js > tmp && mv tmp unified.js – Dominus May 20 '15 at 09:45
  • @Dominus well done! This is the best way to do it, since if `grep` fails, no `mv tmp unified.js` will be performed. – fedorqui May 20 '15 at 09:56
1

I'm late to the party but maybe this helps somebody:

sed 's#\/\/.*##' file

ranemirusG
  • 143
  • 2
  • 16
  • Seems like life was quite simple 7 years ago :), would probably go with webpack today even with custom vanilla projects :D – Dominus Oct 13 '22 at 21:51
0

Piping it through 'grep -v' should work, i.e. something like this (untested):

grep -Ev '^[ \t]*//' file > newfile

Grep's -v flag inverts matches: it prints all lines that do not match a regex to standard output. The lines that do match (your comment lines) are ignored.

Wander Nauta
  • 18,832
  • 1
  • 45
  • 62
  • Only got this one to work with comments that had preceeding whitespaces. – Dominus May 20 '15 at 09:49
  • Ah yes, that should be * not +. Edited. – Wander Nauta May 20 '15 at 09:50
  • Worked for lines beginning with "//" and preceeding whitespaces but still not if there are preceeding tabs :/ seems like removing \t from the regex doesn't make any difference to the result. – Dominus May 20 '15 at 10:03
  • Turns out I was thinking of `-P` not `-E`. It should work with `grep -Pv` - but @fedorqui's regex is better if you want *all* whitespace instead of just tabs and spaces (you probably do). – Wander Nauta May 20 '15 at 10:12