I've got a simple bash script to strip comments from a js file:
#!/bin/bash
sed -E '/^[[:blank:]]*(\/\/|#)/d;s/#.*//' $1 >> stripped.js
this works almost perfect except for comments that occur inline such as
// file-to-be-stripped.js
...
...
const someVar = 'var' // this comment won't be stripped
// this comment will be stripped
what am i missing to strip inline comments?
UPDATE:
What's really strange is i fired up your example with an online bash shell and it works flawlessly! However, when i run the exact same code locally it does not strip the inline ones!? Any idea why / how this could be? I'm obviously missing something... very strange.
Here is my updated code:
My script: stripper.sh
#!/bin/bash
sed -E -e 's:(\s+(//|#)|^\s*(//|#)).*$::; /^$/d' $1 > "stripped.${1}"
My test file: test.js
// testies one
const testies = 'two'
console.log(testies) // three
// testies FOUR!?
console.log('Mmmmm toast') // I won't be stripped of my rights!
Then i execute: ./stripper.sh test.js and the output is:
const testies = 'two'
console.log(testies) // three
console.log('Mmmmm toast') // I won't be stripped of my rights!
Any ideas why running the exact same code locally only sed's whole line comments yet running it with the online bash interpreter (unfortunately I cannot share the exact link to my shell because it is a bit.ly link and apparently that's a "no no" here.) does work as expected?





const url = 'http://stackexchange.com'orconst x = '###'?console.log(something) // some commentstill are not being removed (locally that is. like i said it works perfectly with an online shell) My only guess is maybe different versions of sed? That's my best guess at least... thoughts?sed --versionare you using? try withsed -E -e 's:(\s+(//|#)|^\s*(//|#)).*$::' -e '/^$/d' infile.txtonce--posixflag mine still works but the other behaves exactly as you describe.