CaptainProg
CaptainProg

Reputation: 5690

Find all numbers in text document and manipulate

I have a series of text documents containing the following sort of data:

[...]
Please prepare the subject, then press SPACE
We are testing peripheral vision with a black & white stimulus
45 degree stimulus of 0.728097 logMAR presented
Good response
45 degree stimulus of 0.686705 logMAR presented
Bad response
REVERSAL
Acuity at reversal 7 (experiment 3) was 0.686705 logMAR
135 degree stimulus of 0.732462 logMAR presented
Good response
45 degree stimulus of 0.732462 logMAR presented
[...]

In other words, there's not a lot of uniformity to its format. This data was output by one of my routines that I'm using for my PhD. Stupidly, I got some one of the calculations wrong which means my numbers are all 0.3 lower than they ought to be.

I could go through all my text documents and adjust all the numbers by hand, but there are a lot of documents, each with a lot of numbers.

Can anyone suggest either a pre-existing software, or a suitable approach (I can work with MATLAB, C++, Java or Python) to detect all floating point numbers contained within a text document and add 0.3 to them?

n.b. I am not necessarily looking for a solution that involves me programming anything, but given that this is a pretty bespoke problem, I have a feeling it may involve writing something!

All suggestions gratefully received :)

Upvotes: 0

Views: 119

Answers (2)

panda-34
panda-34

Reputation: 4209

Here's a simple javascript for microsoft engine that does the job

var FSO = WScript.CreateObject("Scripting.FileSystemObject");
var fName = WScript.Arguments(0);
var f = FSO.OpenTextFile(fName, 1);
var fw = FSO.OpenTextFile(fName +".new", 2, true);

while (!f.AtEndOfStream)
    fw.WriteLine(f.ReadLine().replace(/\b\d+\.(\d+)\b/g,
        function($0, $1) {
            return (+$0+0.3).toFixed($1.length);
        }
    ));

f.Close();
fw.Close();

I assume fixed point, no leading or dangling decimal point

Upvotes: 1

shikatozi
shikatozi

Reputation: 1

I do not know of a software that can do this, but your problem could easily be solved by writing a Python script. Simply extract all floating point numbers within X document, add 0.3 to them, and output the result to a new text file. Do this for all documents. Syntax for detecting floating point numbers can simply be found on the web. Hope this helps.

Upvotes: 0

Related Questions