11

私はこの文字列を持っています

-foo {{0.000 0.000} {648.0 0.000} {648.0 1980.0} {0.000 1980.0} {0.000 0.000}}

私はそれを数字に分けて繰り返したいのですが、フィールドセパレーターを使用しようとしましたが成功しませんでした。どうすればawkでそれを行うことができますか?

4

4 に答える 4

17

これをやってみてください:

awk -F'}+|{+| ' '{for (i=1; i<=NF; i++) if ($i ~ "[0-9]") print $i}' file.txt

The Field Separator FS (the -F switch) can be a character, a word, a regex or a class of characters.

You can use this too :

awk 'BEGIN{FS="}+|{+| "} {for(i=1;i<=NF;i++) if($i ~ "[0-9]")print $i}' file.txt

explanations

  • foo|bar|base is a regex where it can match any of the strings separated by the |
  • in }+|{+|, we have the choice to match a literal } at least one : +, or a literal { at least one : +, or a space.
  • you can use a class of character too to do the same : [{} ], both works
于 2013-03-27T17:14:52.367 に答える
1

awk を使用した一方向:

awk -F'[{} ]' '{ for( i=1; i<=NF; i++ ) if( $i ~ /[0-9.]+/ ) print $i }' file

上記の行では、これらの数字を調べましたが、特別なことは何もせず、単に印刷しただけです。その部分にロジックを追加できます。

出力:

0.000
0.000
648.0
0.000
648.0
1980.0
0.000
1980.0
0.000
0.000
于 2013-03-27T17:17:12.610 に答える
1

各数値を新しい行に表示するだけの場合は、次のようにしますgrep

$ egrep -o '[0-9]+\.[0-9]+' file
0.000
0.000
648.0
0.000
648.0
1980.0
0.000
1980.0
0.000
0.000
于 2013-03-28T12:23:55.077 に答える