1

次の形式のファイルがあります。

========================================================
line1line1line1line1line1line1line1line1line1

line2-anything could be here

...anything again

++++++++++++++++++++++++++++++++++++++++++++++++++++++++

========================================================

abc abc abc bacbk kjhhjkh

line2-anything could be here SOME_STRING

...anything again

++++++++++++++++++++++++++++++++++++++++++++++++++++++++

=====*ブロック ( と の間で定義されている) を検索して、存在しない++++*と言うことabcで置き換えたいと考えています。SOME_STRINGたとえば、上記のファイルは次のようになります。

========================================================

abc

++++++++++++++++++++++++++++++++++++++++++++++++++++++++

========================================================

abc abc abc bacbk kjhhjkh

line2-anything could be here SOME_STRING

...anything again

++++++++++++++++++++++++++++++++++++++++++++++++++++++++

ブロック 1 には がありませんSOME_STRING

検索パターンを使用してブロックを取得できます

========================================================\_.\{-\}++++++++++++++++++++++++++++++++++++++++++++++++++++++++

しかし、このパターン検索の結果を grep したいのですが、grep が 1 (一致するものが見つからない) を返す場合は、ブロックを に置き換えabcます。

出来ますか?前もって感謝します。

4

2 に答える 2

1

grep はこれに適したツールではありません。次のように awk を使用します。

awk 'c &&  $0 != "++++"{ l=l $0 RS }
     /====/{ print; c=1 } 
     c && $0 == "++++"{
       if (l ~ /SOME_STRING/)
          print l $0;
       else
          print "abc" RS $0;
       c=0;
       l=""
     }' file

PS: コードを表示するために、上記のコードで切り捨て===================++++++++++++++++長さ 4 にしました。

説明:

/====/{ print; c=1 }    - If input matches "====" then print the line and set c=1
c && !($0 ~ /\+\+\+\+/) - If c=1 and input doesn't match "++++" then don't print but append 
                          current line into a buffer (l=l $0 RS)
c && $0 ~ /\+\+\+\+/ - If c=1 and input matches then execute below block:
   if (l ~ /SOME_STRING/) - if SOME_STRING is in buffer then print buffer + current line
   else                   - print abc + current line
   c=0;l=""               - reset our variables
于 2013-07-04T12:03:09.087 に答える
1

でそれを達成する方法がわからなかったので、投稿を削除してで試してみました:

の内容script.sed:

## For all lines between these patterns...
/^====/,/^++++/ { 
    ## Add current line to "hold space"
    H   

    ## Process next one unless reach to end of range.
    /^++++/! { b } 

    ## Get content of "hold space"
    x   

    ## Remove leading newline added by previous "H" command.
    s/^\n//

    ## If not found the string, do the removal saving either
    ## header and footer.
    /SOME_STRING/! { 
        s/^\(=\+\n\).*\(\n+\+\)$/\1abc\2/ 
    }   

    ## Print and remove all content from both buffers.
    p   
    s/^.*$//
    x   
    s/^.*$//
}

infile次の入力ファイル ( )を想定します。

========================================================
line1line1line1line1line1line1line1line1line1
line2-anything could be here
...anything again
++++++++++++++++++++++++++++++++++++++++++++++++++++++++

========================================================
abc abc abc bacbk kjhhjkh
line2-anything could be here SOME_STRING
...anything again
++++++++++++++++++++++++++++++++++++++++++++++++++++++++

========================================================
abc abc abc bacbk kjhhjkh
line2-anything could be here SOME_STRING
...anything again
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
========================================================
line1line1line1line1line1line1line1line1line1
line2-anything could be here
...anything again
++++++++++++++++++++++++++++++++++++++++++++++++++++++++

次のように実行します。

sed -nf script.sed infile

これにより、次の結果が得られます。

========================================================
abc
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
========================================================
abc abc abc bacbk kjhhjkh
line2-anything could be here SOME_STRING
...anything again
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
========================================================
abc abc abc bacbk kjhhjkh
line2-anything could be here SOME_STRING
...anything again
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
========================================================
abc
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
于 2013-07-04T14:02:31.017 に答える