搜索文件中的号码

时间:2018-12-16 08:21:58

标签: awk

如何用awk或更合适的方式(如果需要)编写程序,它将搜索并写入数字?

我有一个文件

0.0000000  -0.0000000  -0.0000000
   -0.0000000   0.0000000  -0.0000000   0.0000000  -0.0000000  -0.0000000  -0.0000000  -0.0000000   0.0000000   0.0000000   0.0000000
    0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000
    0.0000000   0.0000000   1.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000
    0.0000000  -0.0000000   0.0000000   0.0000000  -0.0000000   0.0000000  -0.0000000   0.0000000   0.0000000  -0.0000000  -0.0000000
   -0.0000000   0.0000000   0.0000000   0.0000000  -0.0000000   0.0000000  -0.0000000  -0.0000000   0.0000000   0.0000000   0.0000000
    0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000
    0.0000000   0.0000000   0.0000000   1.0000000   0.0000000   0.0000000   0.0000000   0.0000000
    0.0000000  -0.0000000   0.0000000   0.0000000  -0.0000000   0.0000000  -0.0000000   0.0000000   0.0000000  -0.0000000  -0.0000000
   -0.0000000   0.0000000   0.0000000   0.0000000  -0.0000000   0.0000000  -0.0000000  -0.0000000   0.0000000   0.0000000   0.0000000
    0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000
    0.0000000   0.0000000   0.0000000   0.0000000   1.0000000   0.0000000   0.0000000   0.0000000
    0.0000000  -0.0000000  -0.0000000  -0.0000000  -0.0000000   0.0000000   0.0000000   0.0000000   0.0000000  -0.0000000  -0.0000000
   -0.0000000   0.0000000   0.0000000   0.0000000  -0.0000000   0.0000000  -0.0000000  -0.0000000  -0.0000000   0.0000000   0.0000000
    0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000
    0.0000000  -0.0000000   0.0000000   0.0000000   0.0000000   1.0000000   0.0000000  -0.0000000
    0.0000000   0.0000000   0.0000000  -0.0000000  -0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000  -0.0000000
   -0.0000000   0.0000000   0.0000000   0.0000000  -0.0000000   0.0000000  -0.0000000  -0.0000000  -0.0000000   0.0000000  -0.0000000
    0.0000000  -0.0000000   0.0000000   0.0000000   0.0000000  -0.0000000  -0.0000000   0.0000000  -0.0000000   0.0000000   0.0000000
    0.0000000  -0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   1.0000000   0.0000000
    0.0000000  -0.0000000   0.0000000   0.0000000  -0.0000000   0.0000000  -0.0000000   0.0000000   0.0000000  -0.0000000  -0.0000000
   -0.0000000   0.0000000  -0.0000000   0.0000000  -0.0000000  -0.0000000  -0.0000000  -0.0000000   0.0000000   0.0000000   0.0000000
    0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000
    0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   0.0000000   1.0000000

 CHECK OF COMPUTED DEPENDENT VARIABLES FROM NORMAL EQUATIONS

   0.0921839
 -10.1319866
   3.6264119
  11.7262830
 -68.8050967
   0.4522270
   0.3373249
 -34.8902892
 -27.7340970
   1.3273557
  23.1789217
 -21.5330918
  25.0183830
 -41.1342361
  -9.4139492
  67.1457087
 -86.1216513
   0.4522270
   0.9082617
  -3.1807870
  -1.9716431
  -1.1840182
  -3.7907675
  -1.4510456
  -1.1857174
  -4.8308521
  -1.3112015
   1.1611722
  -6.1369640
  -3.5550976
  -4.7666450
  -0.6706241
  -2.2754214
  -2.9594634
  -1.9888614
  -0.9457585
  -0.9477964
 -11.1808337
  -9.6823234
  -6.0165193
  -4.6256076
 SUM OF ABSOLUTE VALUES OF CHECKS IS 0.188070D-10
                      Input-Output in F Format

No.  Curve    Input Param.        Correction     Output Param.    Standard Deviation
 9      0     43.8999000000      2.4976090669     46.3975090669      0.3593736560
10      0      0.0883000000     -0.0008250571      0.0874749429      0.0006398321
11      0      2.5816650000     -0.0033525536      2.5783124464      0.0109309501
12      0      1.0000000000      0.0481656121      1.0481656121      0.0288356492
13      0      1.0000000000     -3.1951648165     -2.1951648165      2.0603892225
14      0      0.0000000000     -0.0002127349     -0.0002127349      0.0017416849
15      0      0.2175000000      0.0015097548      0.2190097548      0.0015804101
16      0     80.4198910000     -4.3952312032     76.0246597968      0.1589276670
17      0      1.0000000000     -2.5673410799     -1.5673410799      0.0871801072
18      0      1.0000000000   -205.4932338114   -204.4932338114     14.4204106262
19      0      3.0000000000     -0.2245759531      2.7754240469      0.0822226758
20      0      1.9424000000      0.1257878585      2.0681878585      0.0430855010
21      0      1.0000000000    -18.7946060528    -17.7946060528      2.0771181978
22      0      1.0000000000     -2.8342434886     -1.8342434886      0.2995629339
23      0      3.5047300000     -0.3237559753      3.1809740247      0.0128735907
24      0      5.5942300000      0.6858315083      6.2800615083      0.0486118539
25      0      0.3827000000     -0.0227502151      0.3599497849      0.0041910161

我想得到

46.397509
0.359373
0.087474
0.000639
....

那么如何写类似 如果F格式的字符串Input-Output之后的行以9开头,则以%2.6f的格式写入$ 5(小数点后六位),然后以%2.6f的格式写入$ 6(小数点后六位)

输入文件中有一个类似的表,因此我只需要搜索以该数字开头的第一行。

1 个答案:

答案 0 :(得分:2)

EDIT2: 与OP聊天后,我们知道在Input_file中行匹配Input-Output in F Format之后,需要从Input_file打印第5和第6字段,如下所示。

awk '/Input-Output in F Format/{flag=1;next} flag && ($0 ~ /^[0-9]/ ||  $0 ~ /^ [0-9]+/){printf("%.06f\n%.06f\n",$5,$6)}' Input_file


这是您想要的吗?不清楚,但可以尝试一下。这里的$(NF-1)表示倒数第二个,$NF表示当前行的最后一个字段。因此,您也可以根据需要更改字段编号。

awk 'FNR>4{printf("%.06f\n%.06f\n",$(NF-1),$NF)}' Input_file

输出如下。

46.397509
0.359374
0.087475
0.000640
......... so on.....

编辑: :由于OP的Input_file可能具有TAB分隔数据,因此建议在此处将awk更改为awk -F'\t'到OP。

另外,如果您想在一行中查看列号,则可以执行以下操作。

awk '{for(i=1;i<=NF;i++){print "field_number",i,"field value",$i}}' Input_file

最终解决方案

awk '/^$/{flag=""} /Input-Output in F Format/{flag=1;next} flag && ($0 ~ /^[0-9]/ || $0 ~ /^ [0-9]+/) && ($0 !~ /[2][89]/ && $0 !~ /[3][01]/){printf("%.06f\n%.06f\n",$5,$6)}' Input_file

或现在添加上述解决方案的非一个内衬形式:

awk '
/^$/{
  flag=""
}
/Input-Output in F Format/{
  flag=1
  next
}
flag && ($0 ~ /^[0-9]/ || $0 ~ /^ [0-9]+/) && ($0 !~ /[2][89]/ && $0 !~ /[3][01]/){
  printf("%.06f\n%.06f\n",$5,$6)
}'   Input_file