awkを使用して、5行目から2行目、3行目を移動します。

awkを使用して、5行目から2行目、3行目を移動します。

私のファイルの内容は次のとおりです。

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

awk 'NR==2 && NR==3 {f=$0;next}  NR ==5 &&f{print;print f}' awk.write

ここで問題は、5行以降に2行と3行を移動したい値だけをf保存できるという点だ。nr==3

ベストアンサー1

努力する:

$ awk '
  FNR == 2 { l2 = $0; next } # Save 2nd line
  FNR == 3 { l3 = $0; next } # Save 3rd line
  FNR == 5 {                 # Print 5th line, follow 2nd, 3rd
    print
    print l2
    print l3
    next
  }
  1                          # Print other lines
' <file

ファイルに5行未満があると、2行目と3行目が失われます。

おすすめ記事