Bash

解析 C 風格的多行註釋

  • March 30, 2015

我想過濾未註釋的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 -nsed正則表達式:

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  

正則表達式有問題嗎?

內部註釋 (/* .*? */) 刪除除換行符以外的所有內容;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

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

引用自:https://unix.stackexchange.com/questions/193298