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


8

我的档案中有以下内容

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)-发行版(x86_64-apple-darwin15)


3
用双引号引起来的变量,然后包含换行符。
DisplayName

1
这是YAML,对不对?您是否没有真正使用YAML解析器的任何原因?
查尔斯·达菲

@DisplayName,...要清楚,您的意思是在回显时使用双引号,对吗?
查尔斯·达菲

Answers:


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

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

请注意perl在开始处理整个文件之前先将其保存在内存中,这样解决方案可能不适用于非常大的文件。


7

如果有gawkmawk可以处置,请尝试以下方法:

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

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

说明:它将记录分隔符设置为三个单引号,并在记录号为偶数时打印。

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


(我的)Mac终端默认情况下不知道gawk。
艾默生·科德

4

不像awk的答案那么好,但是就像您最初使用sed一样

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

或更短,如glenn jackman在评论中指出的(略有变化)

/'''/,//{
//!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.

1
您可以压缩sed sed -n "/'''/,//{//!p}"-可能必须set +H先进行bash才能关闭历史记录扩展。
格伦

@glennjackman这就是我将其包含在脚本中的原因,IMO始终更具可读性,并且不受诸如globing,expansion等外壳函数的影响。无论如何,我将其添加到我的答案中,因为它比我的原始脚本更简洁。
2013年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.