세 개의 작은따옴표 사이에 있는 텍스트를 추출합니다.

세 개의 작은따옴표 사이에 있는 텍스트를 추출합니다.

내 파일에 다음 내용이 있습니다

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(파일은 어디에 있습니까?)을 통해 이 부분을 쉘 스크립트로 추출했습니다 .

그러면 콘텐츠가 패드로 포함된 변수가 제공됩니다.

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)를 사용하고 있습니다.

답변1

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

개행 문자가 뒤따르는 '''의 각 쌍 사이의 부분을 추출(및 인쇄)합니다.

perl처리가 시작되기 전에 전체 파일이 메모리에서 사용되므로 이 솔루션은 매우 큰 파일에는 적합하지 않을 수 있습니다 .

답변2

가지고 있거나 액세스할 수 있는 경우 다음을 시도해 보세요 gawk.mawk

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

'''이는 파일에 다른 -s가 없다고 가정합니다.

설명: 레코드 구분 기호를 세 개의 작은따옴표로 설정하고 레코드 번호가 짝수인 경우 인쇄합니다.

awk불행하게도 다중 문자 레코드 구분 기호는 가 아니기 때문에 모든 구현에서 작동하지 않습니다 POSIX awk.

답변3

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.

관련 정보