使用 awk 在第 5 行之后移动第 2 3 行

asa*_*adz 1 awk text-processing

我的文件内容为:

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
Run Code Online (Sandbox Code Playgroud)
awk 'NR==2 && NR==3 {f=$0;next}  NR ==5 &&f{print;print f}' awk.write
Run Code Online (Sandbox Code Playgroud)

这里的问题是,f我只能保存nr==3我想在第 5 行之后移动第 2 行和第 3 行的值。

cuo*_*glm 5

尝试:

$ 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
Run Code Online (Sandbox Code Playgroud)

请注意,如果文件少于五行,您将丢失第二行和第三行。