Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple Field Separators in awk

Tags:

string

regex

awk

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?

like image 447
ilansh Avatar asked Mar 27 '13 17:03

ilansh


People also ask

Can awk use multiple field separators?

As you can see, you can combine more than one delimiter in the AWK field separator to get specific information.

How do you specify field separator in awk?

The variable FS is used to set the input field separator. In awk , space and tab act as default field separators. The corresponding field value can be accessed through $1 , $2 , $3 ... and so on. -F - command-line option for setting input field separator.

How do I pass multiple variables in awk?

awk programming -Passing variable to awk for loop awk: BEGIN -------- <some code here> END{ ----------<some code here> for(N=0; N<H; N++) { for(M=5; M<D; M++) print "\t" D ""; } ----- } ... Discussion started by ctrld and has been viewed 2,402 times.

How do I change the delimiter in awk?

Just put your desired field separator with the -F option in the AWK command and the column number you want to print segregated as per your mentioned field separator.


1 Answers

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
like image 97
Gilles Quenot Avatar answered Nov 08 '22 21:11

Gilles Quenot