Solarisで2つの文字列間の行を削除する

Solarisで2つの文字列間の行を削除する

デフォルトのawkまたはsedを使用して、2つのパターン間のすべての行を削除したいと思います。

foo.txt:

----------------------------------------------------------------------  Cap in MB
line 2
line 3
line 4
line 5
---------------------------------------------------  Cap in MB
----------------------------------------------------------------------  Cap in MB
line 2
line 3
line 4
line 5
---------------------------------------------------  Cap in MB
----------------------------------------------------------------------  Cap in MB
line 2
line 3
line 4
line 5
---------------------------------------------------  Cap in MB
----------------------------------------------------------------------  Cap in MB
line 2
line 3
line 4
line 5
----------------------------------------------------------------------  Cap in MB
line 2
line 3
line 4
line 5

output.txt:

----------------------------------------------------------------------  Cap in MB
Deleted up to this point
----------------------------------------------------------------------  Cap in MB
Deleted up to this point
----------------------------------------------------------------------  Cap in MB
Deleted up to this point
----------------------------------------------------------------------  Cap in MB
line 2
line 3
line 4
line 5
----------------------------------------------------------------------  Cap in MB
line 2
line 3
line 4
line 5

私はSolaris 5.10を使用しており、デフォルトのawkとsedのみを使用しています。スレッドの数は2つのパターンによって異なります。最初の文字列ではなく2番目の文字列を置き換える必要があります。両方のモードの違いはダッシュ数にあります。あなたが見るのはfoo.txt私の本当のファイルです。

ベストアンサー1

 $ awk '!f{print} /----------------------/{f=!f;if (!f)print "Deleted up to this point"}' foo.txt
----------------------------------------------------------------------  Cap in MB
Deleted up to this point
----------------------------------------------------------------------  Cap in MB
Deleted up to this point
----------------------------------------------------------------------  Cap in MB
Deleted up to this point

どのように動作しますか?

スクリプトには変数がありますf。 true(1) の場合、f削除する行の範囲内にあるものです。 false(0) であれば、印刷されるべき範囲にあるものです。

デフォルトfでは、プログラムが起動すると false です。

  • !f{print}

    ffalse の場合、すべての行を印刷します。

  • /----------------------/{f=!f;if (!f)print "Deleted up to this point"}

    破線で示された区切り線に達すると、値が反転しますf。 fがfalseの場合、「削除済み」メッセージを印刷します。

修正する

Solarisのデフォルトawkに問題があるようです。努力する:

nawk '!f{print} /----------------------/{f=!f;if (!f)print "Deleted up to this point"}' foo.txt

または、

/usr/xpg4/bin/awk '!f{print} /----------------------/{f=!f;if (!f)print "Deleted up to this point"}' foo.txt

または、

/usr/xpg6/bin/awk '!f{print} /----------------------/{f=!f;if (!f)print "Deleted up to this point"}' foo.txt

修正された質問に答えてください

$ awk ' /^---------------------------------------------------  Cap in MB/{print "Deleted up to this point"; f=0; z=""; next;} /^----------------------------------------------------------------------  Cap in MB/{f=1; if(z)print substr(z,2); z=""; print;next;}  f{z=z"\n"$0;next;} END{print substr(z,2);}' foo.txt
----------------------------------------------------------------------  Cap in MB
Deleted up to this point
----------------------------------------------------------------------  Cap in MB
Deleted up to this point
----------------------------------------------------------------------  Cap in MB
Deleted up to this point
----------------------------------------------------------------------  Cap in MB
line 2
line 3
line 4
line 5
----------------------------------------------------------------------  Cap in MB
line 2
line 3
line 4
line 5

おすすめ記事