awk - [オフ]の場合、行を変更する

awk - [オフ]の場合、行を変更する

2つの直接行しかない場合は、コマンドを介してファイルの行を変更する方法

~から

Line1
LineA
OldLine

到着

Line1
LineA
NewLine

ベストアンサー1

非常に一般的な入力/出力を考慮すると、これは機能します。

awk '{ 
        if (f == 2) 
        {
           print "NewLine"
           next
        } else if (/Line1/) 
        { 
           f=1 
        } else if (f == 1 && /LineA/)
        {
           f=2
        } else 
        { 
           f=0 
        } 
        print 
     }'

一行形式で。

awk '{if (f == 2) { print "NewLine"; next } else if (/Line1/) { f=1 } else if (f == 1 && /LineA/) { f=2 } else { f=0 } print }'

いくつかの例。

$ echo -en 'Line1\nLineA\nOldLine'|awk '{if (f == 2) { print "NewLine"; next } else if (/Line1/) { f=1 } else if (f == 1 && /LineA/) { f=2 } else { f=0 } print }'
Line1
LineA
NewLine

$ echo -en 'Line1\nLineB\nOldLine'|awk '{if (f == 2) { print "NewLine"; next } else if (/Line1/) { f=1 } else if (f == 1 && /LineA/) { f=2 } else { f=0 } print }'
Line1
LineB
OldLine

おすすめ記事