awkを使用して2行を連続してコピーし、3行目をスキップします。

awkを使用して2行を連続してコピーし、3行目をスキップします。

とても簡単ですawk

awk '(NR%3)' awk.write

このファイルの場合:

this line 1 no un1x
this lines 22 0
butbutbut this 33 22 has unix
but not 1
THIS is not
butbutbut ffff
second line

私の結果は次のとおりです

this line 1 no un1x
this lines 22 0
but not 1
THIS is not
second line

ただし、最後の行は連続の定義を満たしていないため、必要ありません。

3つの連続行ごとに最初の2行を取得するにはどうすればよいですか?

ベストアンサー1

変数を使用して、前の行が存在するかどうかを追跡できます。

$ awk '
  FNR % 3 == 1 {f = $0; next}  # The first line keep in f, skip to next line
  FNR % 3 && f {print f;print} # Previous line present, print it and current line
' <file
this line 1 no un1x
this lines 22 0
but not 1
THIS is not

または以下を使用してsed

sed -ne 'N;/\n/p;N;d' <file

おすすめ記事