【问题标题】:How to capture certain group of text with sed?如何使用 sed 捕获某些文本组?
【发布时间】:2023-03-07 19:11:02
【问题描述】:

这是交易,我正在尝试使用下面的jsdoc 捕获所有函数(在return 函数内),但我一直没有这样做。

到目前为止,我尝试过

cat test.js | sed -n 's/.*\(\/\*\).*/\1/p'

但我得到的只是 /*

我想找回这个:

   /**
    * Gets the Confirm button
    *
    * @returns {button>}
    */
    function getConfirmButton() {
        return find(button);
    }

这是我试图从中提取文本的文件。

import sth;
const stuff = () => {

    return {
        /**
        * Gets the Confirm button
        *
        * @returns {button>}
        */
        function getConfirmButton() {
            return find(button);
        }
    }
}

【问题讨论】:

  • 你的逻辑不清楚(至少对我来说)。您想打印从第一个“/*”到...下一对右大括号的所有内容,只用空格分隔?

标签: bash sed jsdoc


【解决方案1】:
sed -rzn s'/(^.*)(\/\*\*.*find\(button\)\;[[:space:]]+})(.*$)/\2/p' test.js

使用 -z 将文件作为一行使用。使用正则表达式 (-r) 将文件分成 3 个部分。替换第二部分的行并打印。

【讨论】:

    【解决方案2】:

    假设:

    • OP 没有使用jsdoc-aware 工具来解析数据是有原因的(例如,How to parse JSDoc
    • 虽然示例仅显示了在 return 函数中定义的一个(子)函数...
    • 来自 OP 评论 - “捕获所有功能 ...(在 return 功能内)” - 我认为这意味着可能有超过 1 个功能被退回
    • OP 可以返回存在于return {} 边界内的所有内容(例如,函数、cmets、空行)
    • 源在语法上是正确的(例如,所有左大括号 ({) 都有一个匹配的右大括号 (})
    • 在我们希望显示的代码块之外,return 这个词只出现在我们感兴趣的一个函数的名称中

    示例输入(使用一些“额外”函数和 cmets 来演示解决方案):

    $ cat test.js
    import sth;
    const stuff = () => {
    
        return {
            /**                                                  # first line of output
            * Gets the Confirm button
            *
            * @returns {button>}
            */
            function getConfirmButton() {
                return find(button);
            }
        another_function () { some stuff }
    
          and_one_more_function() {
           with some lines
    in here that are total
       rubbish }
         /** some extra comments
          **/                                                    # last line of output
        }                                                        # matches with "return {"
    }
    

    一个基于计算左右大括号的awk解决方案({ => +1/} => -1):

    awk '
    /return.*{/ { count=1 ; next }                               # start of the parent function "return"; left brace sets count = 1
    ! count     { next }                                         # if count == 0 => not interested in this line of input
                { split($0,arr,"[{}]",delims)                    # split line by delimiters "{" and "}", saving actual delimiters in the delims[] array
                  for ( i in delims )                            # loop through list of delimiters
                      ( delims[i] == "{" ) ? count++ : count--   # increment count for each "{" and decrement count for each "}"
                  if (count) print                               # if count > 0 then print the current line
                }
    ' test.js
    

    这会生成:

            /**
            * Gets the Confirm button
            *
            */
            function getConfirmButton() {
                return find(button);
            }
        another_function () { some stuff }
    
          and_one_more_function() {
           with some lines
    in here that are total
       rubbish }
         /** some extra comments
          **/
    

    【讨论】:

      【解决方案3】:

      基于终止 } 之前的空白,匹配开始 return 之前的空白,第三个参数为 GNU awk,match()\s 简写为[[:space:]]

      $ awk '
           f { if (index($0,end)==1) f=0; else print; next }
           match($0,/^(\s*)return/,a) { f=1; end=a[1]"}" }
      ' file
              /**
              * Gets the Confirm button
              *
              * @returns {button>}
              */
              function getConfirmButton() {
                  return find(button);
              }
      

      或使用任何 awk:

      $ awk '
          f { if (index($0,end)==1) f=0; else print; next }
          match($0,/^[[:space:]]*return/) { f=1; end=substr($0,1,RLENGTH-6)"}" }
      ' file
              /**
              * Gets the Confirm button
              *
              * @returns {button>}
              */
              function getConfirmButton() {
                  return find(button);
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-06-30
        • 2011-02-16
        • 1970-01-01
        • 1970-01-01
        • 2017-01-21
        • 2013-11-14
        • 2016-03-13
        相关资源
        最近更新 更多