Extract numbers with awk

a part of a script in awk looks like this

sub(/^Mutation: {"seq-pos":/,"") && sub(/, "time":/," ") && sub(/}$/,"") { print >"fifth_"FILENAME}

The aim is that given this input:

Mutation: {"seq-pos":0.00030804, "time":0, 0.164494, 1.00723}
Mutation: {"seq-pos":0.176236, "time":0.31516, 0.6876, 1.00723}
Mutation: {"seq-pos":0.224808, "time":0, 0.813626, 1.00723}

I can get the following output:

0.00030804 0 0.164494 1.00723
0.17623 0.31516 0.6876 1.00723
0.224808 0 0.813626 1.00723

the code works if there is only 1 number after it. How would I need to mod

the whole script ...

!body && /^\/\/$/ {body=1}
body && sub(/^gthcont: */,"") {print > "second_"FILENAME}
body && /^[01]+/ {print > "third_"FILENAME}
body && /^\[[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\]/ { print > "first_"FILENAME print substr($0, 2, index($0,"]")-2) > "fourth_"FILENAME
}
sub(/^Mutation: {"seq-pos":/,"") && sub(/, "time":/," ") && sub(/}$/,"") { print >"fifth_"FILENAME}

ify?

1

2 Answers

I would suggest defining multiple field separators (i.e. :, ,, and }) and then select the fields accordingly:

awk 'BEGIN { FS = "[:,}]" } { print $3,$5,$6,$7 } ' 

What about removing everything not being a digit, a dot or a space?

$ awk '{gsub(/[^0-9. ]/,"")}1' file 0.00030804 0 0.164494 1.00723 0.176236 0.31516 0.6876 1.00723 0.224808 0 0.813626 1.00723

Note however that parsing a JSON with awk is not a very good approach. You may want to use jq for this.

10

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like