ktoui
ktoui

Reputation: 73

awk reading in values

Hello the following code is used by me to split a file

BEGIN{body=0}
!body && /^\/\/$/    {body=1}
body  && /^\[/       {print > "first_"FILENAME}
body  && /^pos/{$1="";print > "second_"FILENAME}
body  && /^[01]+/    {print > "third_"FILENAME}
body  && /^\[[0-9]+\]/ {
  print > "first_"FILENAME
  print substr($0, 2, index($0,"]")-2) > "fourth_"FILENAME
}

the file looks like here

 header
//
SeqT: {"POS-s":174.683, "time":0.0130084}
SeqT: {"POS-s":431.49, "time":0.0221447}
[2.04545e+2]:0.00843832,469:0.0109533):0.00657864,((((872:0.00120503,((980:0.0001);
[29]:((962:0.000580339,930:0.000580339):0.00543993);
absolute:
gthcont: 5 4 2 1 3 4 543 5  67 657  78 67 8  5645 6 
01010010101010101010101010101011111100011
1111010010010101010101010111101000100000
00000000000000011001100101010010101011111

The problem is that in the file 4 print substr($0, 2, index($0,"]")-2) > "fourth_"FILENAME the number with the sci notation with e does not get through. it works only as long as it is written without that . how can i cahnge the awk to also get the number in the way like 2.7e+7 or so

Upvotes: 0

Views: 57

Answers (1)

John B
John B

Reputation: 3646

The problem is you're trying to match E notation when your regex is looking for integers only.

Instead of:

/^\[[0-9]+\]/

use something like:

/^\[[0-9]+(\.[0-9]+(e[+-]?[0-9]+)?)?\]/

This will match positive integers, floats, and E notation wrapped in square brackets at the start of the line.

See demo

Upvotes: 2

Related Questions