私はこの文字列を持っています
-foo {{0.000 0.000} {648.0 0.000} {648.0 1980.0} {0.000 1980.0} {0.000 0.000}}
私はそれを数字に分けて繰り返したいのですが、フィールドセパレーターを使用しようとしましたが成功しませんでした。どうすればawkでそれを行うことができますか?
これをやってみてください:
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
foo|bar|base
is a regex where it can match any of the strings separated by the |
}+|{+|
, we have the choice to match a literal }
at least one : +
, or a literal {
at least one : +
, or a space.[{} ]
, both worksawk を使用した一方向:
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
各数値を新しい行に表示するだけの場合は、次のようにします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