データファイルfile1.txtがあり、パターン(「最適フィットポイント」または「最適フィット」)を一致させ、その行の最後の列を印刷しようとしています。
ファイル1.txt
best-fit point: 0.0000 0.0000 0.0000 x coordinate 0.0000
best-fit point: -0.4330 -0.2500 0.0884 x coordinate 0.5078
best-fit point: 0.8660 0.5000 -0.1768 x coordinate 2.0310
best-fit point: -0.4330 0.2500 0.1768 x coordinate 3.4003
best-fit point: 0.8660 1.0000 -0.0884 x coordinate 4.9236
best-fit point: 0.4330 0.7500 0.0000 x coordinate 5.4313
best-fit point: 0.8660 -0.5000 -0.3536 x coordinate 6.8006
best-fit point: 0.0000 0.0000 0.2652 x coordinate 7.9766
best-fit point: 1.2990 0.7500 0.0000 x coordinate 9.4998
best-fit point: 0.8660 0.5000 0.0884 x coordinate 10.0076
best-fit point: 1.2990 -0.7500 -0.2652 x coordinate 11.3769
best-fit point: 0.8660 -1.0000 -0.1768 x coordinate 11.8846
best-fit point: 0.0000 0.0000 -0.5303 x coordinate 13.2539
出力
0.0000
0.5078
2.0310
3.4003
4.9236
5.4313
6.8006
7.9766
9.4998
10.0076
11.3769
11.8846
13.2539
私の解決策と問題
awk '/best-fit point:/{i==0 ; i++; print "K"i"="$8}' file-1.txt
列に負のデータがない場合、awk コマンドは正常に実行されます。パターンマッチング後に最後の列を印刷する他の方法はありますか?
よろしくお願いします!
ベストアンサー1
入力した最後の列が常に前の列とは別の場合は、フィールド番号を参照して最後の列を出力するようにAWKに要求できます。
awk '/best-fit point:/ { printf "%7.4f\n", $NF }'
あなたの質問に表示された出力が生成されます。
しかし、入力内容は固定幅の列を使用しているようです。 GNU AWKを使用している場合は、FIELDWIDTHS
次のように列を指定できます。
awk -vFIELDWIDTHS="21 7 7 7 16 8" '/best-fit point:/ { printf "%7.4f\n", $6 }'