2015-05-11 58 views
0

我有一個文件,內容如下。我想編輯這個文件,以便無論何時在文件中看到LTPGY模式,然後在「set t table new $ m table」行之後,插入一個帶有「FOUND」內容的新行。如何在2個特定模式匹配後編輯一個Unix文件插入幾行

輸入文件如下: -

set ra [ 1 22 3 ] LTPGY 
    set a 0 
    set a1 1 

    set t [ table new $m table] 
    set a [ $t process_axis] 
    set a [ $t voltage_axis] 



    set ra [ 1 22 3 ] STPGY 
    set a 0 
    set a1 1 

    set t [ table new $m table] 
    set a [ $t process_axis] 
    set a [ $t voltage_axis] 


    set ra [ 1 22 3 ] LTPGY 
    set a 0 
    set a1 1 

    set t [ table new $m table] 
    set a [ $t process_axis] 
    set a [ $t voltage_axis] 



    set ra [ 1 22 3 ] STPGY 
    set a 0 
    set a1 1 

    set t [ table new $m table] 
    set a [ $t process_axis] 
    set a [ $t voltage_axis] 

我所要的輸出是這樣的: -

set ra [ 1 22 3 ] LTPGY 
    set a 0 
    set a1 1 

    set t [ table new $m table] 
    FOUND 
    set a [ $t process_axis] 
    set a [ $t voltage_axis] 



    set ra [ 1 22 3 ] STPGY 
    set a 0 
    set a1 1 

    set t [ table new $m table] 
    set a [ $t process_axis] 
    set a [ $t voltage_axis] 


    set ra [ 1 22 3 ] LTPGY 
    set a 0 
    set a1 1 

    set t [ table new $m table] 
    FOUND 
    set a [ $t process_axis] 
    set a [ $t voltage_axis] 



    set ra [ 1 22 3 ] STPGY 
    set a 0 
    set a1 1 

    set t [ table new $m table] 
    set a [ $t process_axis] 
    set a [ $t voltage_axis] 
+2

這有點寬泛。你能指出輸出文件的樣子嗎?此外,分享您的嘗試也是一件好事 – fedorqui

回答

1

工程與GAWK,其他awks未經測試:

gawk ' 
    /set ra/{found = /LTPGY/} 
    1; 
    index($0, "set t [ table new $m table]") && found {print "FOUND"} 
' file 
1

這可能適用於你(GNU sed):

sed '/LTPGY/{:a;n;/set t \[ table new $m table\]/!ba;p;s/\S.*/FOUND/}' file 

將注意力集中在LTPGY後面的行上並找到所需的字符串,然後打印它,然後用所需字符串FOUND替換除前導空格以外的所有字符。

相關問題