使用 awk 跳过输入文件的第一行

skip the first line of input file with awk

我正在尝试使用此脚本从一些输入文件中提取数据。问题是输入文件的第一行有一个 header 并且这一行也出现在结果中。该脚本执行它应该执行的操作。如何跳过输入文件的第一行?

#!/bin/awk -f 
NF == 3 {               ## 3-fields - heading row
if (FNR>1)                ## not 1st row
print ""                ##  output newline
printf "%d", ++n          ## output counter 1, 2, 3, ...
for (i=1; i<=NF; i++)     ## loop over each heading
printf "  %s", $i       ##  output with 2-leading spaces
next                      ## skip to next record
}

{
  printf "%s", [=10=]           ## output all recs until next heading
}
END {
  print ""                  ## END rule - tidy up with newline.
}

我试过这个:

#!/bin/awk -f 
NR>1{
NF == 3 {               ## 3-fields - heading row
if (FNR>1)                ## not 1st row
print ""                ##  output newline
printf "%d", ++n          ## output counter 1, 2, 3, ...
for (i=1; i<=NF; i++)     ## loop over each heading
printf "  %s", $i       ##  output with 2-leading spaces
next                      ## skip to next record
}

{
  printf "%s", [=11=]           ## output all recs until next heading
}
}
END {
  print ""                  ## END rule - tidy up with newline.
}

但这不是正确的语法。

输入示例如下:

 &plot nbnd=  60, nks=    93 /
            0.000000  0.000000  0.000000
  -44.871  -44.869  -21.466  -21.462  -21.364  -21.275  -21.275  -21.271   -6.942   -6.316
   -5.786   -5.623   -5.519   -5.488   -4.009   -3.947   -3.935   -3.935   -3.927   -3.886
   -3.822   -3.723   -3.691   -3.661    4.013    5.337    6.511    7.180    7.181    7.730
    7.755    7.826    8.312    8.450    8.932    8.932    9.076    9.157    9.397    9.542
    9.633    9.909    9.909   11.553   13.243   14.431   14.431   14.547   14.566   14.914
   15.137   16.017   16.135   16.410   17.399   17.960   18.076   18.319   19.480   19.480
            0.000000  0.100000  0.000000
  -44.871  -44.869  -21.466  -21.462  -21.364  -21.277  -21.275  -21.273   -6.928   -6.303
   -5.783   -5.621   -5.517   -5.492   -4.007   -3.940   -3.938   -3.934   -3.928   -3.888
   -3.820   -3.721   -3.690   -3.659    3.993    5.281    6.513    7.189    7.191    7.645
    7.766    7.901    8.255    8.447    8.922    8.934    9.017    9.216    9.401    9.543
    9.674    9.869    9.915   11.517   13.293   14.377   14.421   14.434   14.706   14.913
   15.138   16.093   16.158   16.424   17.461   17.957   18.043   18.282   19.271   19.450
            0.000000  0.200000  0.000000
  -44.871  -44.869  -21.466  -21.461  -21.363  -21.283  -21.280  -21.274   -6.888   -6.266
   -5.773   -5.613   -5.510   -5.505   -4.001   -3.947   -3.934   -3.929   -3.920   -3.892
   -3.816   -3.714   -3.685   -3.654    3.933    5.132    6.519    7.210    7.222    7.539
    7.761    7.991    8.095    8.443    8.879    8.937    8.941    9.377    9.402    9.539

如果输入保存在input.dat我运行awk脚本如下:

./script.awk input.dat > output.dat

我一直在阅读 awk 的文档,但我只是不理解这些脚本的结构(如上面的脚本)。他们看起来真的和

不一样
BEGIN {

}
{

}
END {

}

结构。我对 awk 很迷茫,我知道我的问题很烦人...

skip the first line of input file with awk

首先使用 FNR==1{next} pattern-action,将其应用于您的第一个代码会得到

#!/bin/awk -f
FNR==1{next}
NF == 3 {               ## 3-fields - heading row
print ""                ##  output newline
printf "%d", ++n          ## output counter 1, 2, 3, ...
for (i=1; i<=NF; i++)     ## loop over each heading
printf "  %s", $i       ##  output with 2-leading spaces
next                      ## skip to next record
}

{
  printf "%s", [=10=]           ## output all recs until next heading
}
END {
  print ""                  ## END rule - tidy up with newline.
}