解析 C 风格多行注释

解析 C 风格多行注释

我想过滤未注释的 javascriptsed并输出行号

这是示例:

/*!
* 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
*/

有人告诉我像这样 使用grep -n和正则表达式:sed

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

它给我输出:

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  

但我想要输出:

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  

正则表达式有问题吗?

答案1

内部注释 (/* .*? */) 删除除换行符之外的所有内容; grep 非空行:

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

其输出:

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

答案2

使用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

相关内容