提取三个单引号之间的文本

提取三个单引号之间的文本

我的文件中有以下内容

description: '''
        This rule forbids throwing string literals or interpolations. While
        JavaScript (and CoffeeScript by extension) allow any expression to
        be thrown, it is best to only throw <a
        href="https://developer.mozilla.org
        /en/JavaScript/Reference/Global_Objects/Error"> Error</a> objects,
        because they contain valuable debugging information like the stack
        trace. Because of JavaScript's dynamic nature, CoffeeLint cannot
        ensure you are always throwing instances of <tt>Error</tt>. It will
        only catch the simple but real case of throwing literal strings.
        <pre>
        <code># CoffeeLint will catch this:
        throw "i made a boo boo"

        # ... but not this:
        throw getSomeString()
        </code>
        </pre>
        This rule is enabled by default.
        '''

以及该文件中的其他一些内容。

sed -n "/'''/,/'''/p" $1我通过($1文件在哪里)将这部分提取到我的 shell 脚本中。

这给了我一个变量,其内容作为一个衬垫

description: ''' This rule forbids throwing string literals or interpolations. While JavaScript (and CoffeeScript by extension) allow any expression to be thrown, it is best to only throw <a href="https://developer.mozilla.org /en/JavaScript/Reference/Global_Objects/Error"> Error</a> objects, because they contain valuable debugging information like the stack trace. Because of JavaScript's dynamic nature, CoffeeLint cannot ensure you are always throwing instances of <tt>Error</tt>. It will only catch the simple but real case of throwing literal strings. <pre> <code># CoffeeLint will catch this: throw "i made a boo boo" # ... but not this: throw getSomeString() </code> </pre> This rule is enabled by default. '''

我现在如何提取 之间的部分'''

或者是否有更好的方法从多行文件中检索它?

我使用的是 Mac El Captain 10.11.2 和 GNU bash,版本 3.2.57(1)-release (x86_64-apple-darwin15)

答案1

perl -l -0777 -ne "print for /'''(.*?)'''/gs" file

将提取(并打印后跟换行符)每对 ''' 之间的部分。

请注意,perl在开始处理之前会占用内存中的整个文件,因此该解决方案可能不适合非常大的文件。

答案2

如果您有gawkmawk可以使用,请尝试以下操作:

gawk -v "RS='''" 'FNR%2==0' file

'''这假设文件中没有其他-s。

说明:它将记录分隔符设置为三个单引号,如果记录号是偶数则打印。

不幸的是,它不适用于所有awk实现,因为多字符记录分隔符不是POSIX awk.

答案3

不像 awk 答案那么好,但因为你最初使用的是 sed

/'''/{
   s/.*'''//
   :1
   N
   /'''/!b1
   s/'''.*//
   p
}
d

或者按照格伦·杰克曼在评论中指出的更短(略有改变)

/'''/,//{
//!p
}
d

运行为

sed -f script file

输出

    This rule forbids throwing string literals or interpolations. While
    JavaScript (and CoffeeScript by extension) allow any expression to
    be thrown, it is best to only throw <a
    href="https://developer.mozilla.org
    /en/JavaScript/Reference/Global_Objects/Error"> Error</a> objects,
    because they contain valuable debugging information like the stack
    trace. Because of JavaScript's dynamic nature, CoffeeLint cannot
    ensure you are always throwing instances of <tt>Error</tt>. It will
    only catch the simple but real case of throwing literal strings.
    <pre>
    <code># CoffeeLint will catch this:
    throw "i made a boo boo"

    # ... but not this:
    throw getSomeString()
    </code>
    </pre>
    This rule is enabled by default.

相关内容