multiple Field Separators in awk
26,187
Solution 1
Try doing this :
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
Solution 2
One way with awk :
awk -F'[{} ]' '{ for( i=1; i<=NF; i++ ) if( $i ~ /[0-9.]+/ ) print $i }' file
In the line above, we went through those numbers, but I didn't do anything special, just printed them. You could add your logic to that part.
Output:
0.000
0.000
648.0
0.000
648.0
1980.0
0.000
1980.0
0.000
0.000
Solution 3
If you just want to display each number on a new line then simply use 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
Author by
ilansh
Updated on November 26, 2020Comments
-
ilansh over 3 years
i have this string
-foo {{0.000 0.000} {648.0 0.000} {648.0 1980.0} {0.000 1980.0} {0.000 0.000}}
i want to separate it to numbers and iterate over them ,thanks tried to use Field separator without success how can i do it with awk?
-
ilansh about 11 yearsthanks sputnick can ypu please explain the '}+|{+| ' seperator
-
Qian Chen about 9 years@stArdustͲ I want to get either a colon or spaces as the separator, however, it seems awk -F ':| ' doesn't work. Am I missing anything? Thanks.
-
Qian Chen about 9 yearsI figured out the answer: awk -F ':[ \t]*|[ \t]+'. Thanks to this link: softpanorama.org/Tools/Awk/awk_regular_expressions.shtml