Eme*_*Cod 8 shell-script text-processing
我在文件中有以下内容
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.
'''
Run Code Online (Sandbox Code Playgroud)
与此文件中的其他一些内容。
我通过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. '''
Run Code Online (Sandbox Code Playgroud)
我现在如何提取 之间的部分'''
?
或者有没有更好的方法从多行文件中检索它?
我在 Mac El Captain 10.11.2 和 GNU bash,版本 3.2.57(1)-release (x86_64-apple-darwin15)
Sté*_*las 12
perl -l -0777 -ne "print for /'''(.*?)'''/gs" file
Run Code Online (Sandbox Code Playgroud)
将提取(并打印后跟换行符)每对 ''' 之间的部分。
请注意,perl
在开始处理之前将整个文件存储在内存中,因此该解决方案可能不适用于非常大的文件。
试试这个,如果你有gawk
或mawk
可以使用:
gawk -v "RS='''" 'FNR%2==0' file
Run Code Online (Sandbox Code Playgroud)
这假设'''
文件中没有其他-s。
说明:它将记录分隔符设置为三个单引号,如果记录数为偶数则打印。
不幸的是,它不适用于所有awk
实现,因为多字符记录分隔符不是POSIX awk
.