2013-07-04 31 views
1

我在以下格式的文件:的grep在搜索模式和替換如果模式不存在

======================================================== 
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更換塊。

可能嗎?提前致謝。

回答

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 
+0

感謝您的答覆,但它是不可能的SED或VIM?我不明白awk。 – Saurabh

+1

awk很神祕。你能不能概述一下你的方法?沒有這些,罐裝解決方案中的學習很少。 –

+0

@IngoKarkat:如果awk很神祕,那麼sed將會變得更加神祕:P – anubhava

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 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
+0

我想這實際上會取代字符串SOME_STRING塊abc。我想替換SOME_STRING **不存在的塊**。運算符[^ some_char]對於角色來說工作得很好,但是在這裏我需要「除了SOME_STRING以外的任何東西」 – Saurabh

+0

@SaurabhDhir:哦,這是真的。抱歉。我會盡力解決它。 – Birei

+0

@SaurabhDhir:我已經用'sed'結尾而不是'vim'。對我來說似乎更容易。 – Birei