1

I want to filter non-commented javascript with sed and output the line number

Here is the example:

/*!
* jQuery UI 1.8.17
*
* Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
* http://docs.jquery.com/UI
*/(function(a,b){function d(b)       {return!a(b).parents().andSelf().filter(f/*!
* jQuery UI Widget 1.8.17
*
* Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
* http://docs.jquery.com/UI/Widget
*/(function(a,b){if(a.cleanData){var  c=a.cleanData;a.cleanData=function(b/*!
* jQuery UI Mouse 1.8.17
*
* Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
* http://docs.jquery.com/UI/Mouse
*
* Depends:
*   jquery.ui.widget.js
*/

Somebody told me to use grep -n and sed regex like this:

grep -n "" test.js | sed ':a;$!N;$!ba;s/\/\*[^*]*\*\([^/*][^*]*\*\|\*\)*\///g'

It give me output:

1:(function(a,b){function d(b){return!a(b).parents().andSelf().filter(f(function(a,b){if(a.cleanData){var c=a.cleanData;a.cleanData=function(b  

But I want output:

9: (function(a,b){function d(b){return!a(b).parents().andSelf().filter(f
17: (function(a,b){if(a.cleanData){var c=a.cleanData;a.cleanData=function(b  

Is there something wrong with the regex?

2 Answers2

1

Inside comments (/* .*? */) remove everything except newlines; grep non empty lines:

perl -p0E 's!(/\*.*?\*/)!$1 =~ s/.//gr!egs;' test.js |grep -nP '\S'

which outputs:

9:(function(a,b){function d(b)       {return!a(b).parents().andSelf().filter(f
17:(function(a,b){if(a.cleanData){var  c=a.cleanData;a.cleanData=function(b
JJoao
  • 12,170
  • 1
  • 23
  • 45
0

Using Awk:

awk '/^*\/\(/ {gsub(/\*\/|\/\*!/,""); print NR":",$0}' js
9: (function(a,b){function d(b)       {return!a(b).parents().andSelf().filter(f
17: (function(a,b){if(a.cleanData){var  c=a.cleanData;a.cleanData=function(b
jasonwryan
  • 73,126