I have a file with this kind of structure: Some text a = 342 b = 222 other text c = 9834 Some text (then it repeats)

From my googling I suspect it can be done with Find[] after creating a stream from this file. But I could use some help, as I'm sure this must be a common problem with a commong solution.

  • 3
    $\begingroup$ Would TextCases[string, "Number"] be helpful? $\endgroup$
    – Michael E2
    Oct 8, 2016 at 0:10
  • $\begingroup$ It might be. In fact, I think I can also use readlist. The advantage of this file is that the order never changes so I could setup a loop that sorts and attributes to arrays. I'll give that a shot on monday. $\endgroup$
    – Diego
    Oct 9, 2016 at 1:43

1 Answer 1


Here's my own solution. It certainly isn't the only way to do it but it's the one I've found. Hopefully it may be of some use to others.

This solution works when you want extract numerical data from a file that is made to be human readable ("measured quantities: variable x = 0.333" etc) and it follows a pattern. So it wouldn't work for a dissertation or article, obviously. Take for example this file:

Before iteration
a = 0.2
b = 9
After Iteration
c = 34983
d = 34873847837
Before iteration
a = 33
b =...

The main idea is to use Import[filename, "Data"], then each line will be an element of this list, which you can use ToExpression[StringSplit[Data[[(appropriate index]]][[3]], where "3" would be the number after the equal sign in this case. It's just a matter to create a loop that covers the entire file saving the information you want in the arrays you want. Like this:

Data = Import[filename, "Data"],
j = 0,
b = {},
 If[(1 + j*22) + 1 > Length[Data], {Break[]}];
 b = Join[b, {ToExpression[
     StringSplit[Data[[(1 + j*6) + 1]]][[3]]]}];

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.