# How to import complex .txt file to matrix in MMA for user defined keywords / database?

I am thinking about how can we import complex .txt file (Ansys/Abaqus) to MMA for the selected database?

For example: txt file (input file):

 ...
...
10894, 10895, 10896, 10897, 10898, 10899, 10900, 10901, 10902, 10903, 10904, 10905, 10906, 10907, 10908, 10909
10910, 10911, 10912, 10913, 10914, 10915, 10916, 10917, 10918, 10919, 10920, 10921, 10922, 10923, 10924, 10925
10926, 10927
10122,  10534,      1
*Gaaf, nlau=Saoot-24
29,   30,   31,   32,   33,   34,  400,  401,  402,  403,  404,  405,  406,  407,  408,  409
410,  411,  412,  413,  414,  415,  416,  439,  440,  441,  442,  443,  444,  445,  446,  447
3157, 3158, 3159, 3160, 3161, 3162, 3163, 3164, 3165, 3166, 3167, 3168, 3169, 3170, 3171, 3172
3173, 3174, 3175, 3176, 3177, 3178
1477,  1782,     1
*Gaaf, nlau=Saoot-25
32,   33,   76,   86,  455,  456,  457,  458,  459,  460,  461,  462,  463,  464,  465,  466
9611, 9612, 9613, 9614, 9615, 9616, 9617, 9618, 9619, 9620, 9621, 9622, 9623, 9624, 9625, 9626
9627, 9628, 9629, 9630
8891,  9060,     1
*Gaaf, nlau=Saoot-26
24,   25,   28,   29,   30,  344,  345,  346,  347,  348,  349,  350,  351,  352,  353,  354
355,  356,  387,  388,  389,  390,  391,  392,  393,  394,  395,  396,  397,  398,  399,  400
...
...


What do we need is such kind of output in MMA (matrix):

10122 10534 23
1477 1782 24
8891 9060 25


Import rule is: we find *Edfsf line: the value after the key words Saoot- and the following line two values: namely 10122 10534 23 then we repeat the rule for the next same key words.

Sure, if I import all the data in MMA, it is ok, but how to import selected information in MMA, at least not clear for me now.

• Is importing the entire file and then processing it acceptable or does the solution have to read the file line by line and filter/construct the required output? – Rohit Namjoshi Nov 20 '18 at 4:17
• Where does this file come from? Can you add some background information? – xzczd Nov 20 '18 at 5:28
• @RohitNamjoshi, the second option:the solution have to read the file line by line and filter/construct the required output. – ABCDEMMM Nov 20 '18 at 10:43
• @xzczd , abaqus/ansys/codeaster and so on. – ABCDEMMM Nov 20 '18 at 10:43
• Is it possible to export a more regular file (say, a binary file arranged according to a certain template) from these softwares? – xzczd Nov 20 '18 at 11:46

I know nothing about abaqus/ansys/codeaster, but I suspect the .txt file illustrated in the question isn't the best choice for exporting data from these softwares. Anyway, the following is a possible solution if you have to live with the .txt file:

read[stream_] :=
Last@Last@Reap@(While[True,
Sow@{StringCases[If[# === EndOfFile, Break[], #] &@Find[stream, {"Edfsf"}],
"Saoot-" ~~ a__ ~~ "," :> ToExpression@a][[1]],


The input of read function is a stream, which you can create with OpenRead function in your real case. Here I'll simply illustrate with StringToStream:

string = "*Edfsf, cadifd=Saoot-23, coming
10122,  10534,      1
*Gaaf, nlau=Saoot-24
29,   30,   31,   32,   33,   34,  400,  401,  402,  403,  404,  405,  406,  407,  \
408,  409
410,  411,  412,  413,  414,  415,  416,  439,  440,  441,  442,  443,  444,  445,  \
446,  447
3157, 3158, 3159, 3160, 3161, 3162, 3163, 3164, 3165, 3166, 3167, 3168, 3169, 3170, \
3171, 3172
3173, 3174, 3175, 3176, 3177, 3178
1477,  1782,     1
*Gaaf, nlau=Saoot-25
32,   33,   76,   86,  455,  456,  457,  458,  459,  460,  461,  462,  463,  464,  \
465,  466
9611, 9612, 9613, 9614, 9615, 9616, 9617, 9618, 9619, 9620, 9621, 9622, 9623, 9624, \
9625, 9626
9627, 9628, 9629, 9630

• @ABCDEMMM As mentioned above, please check the document of OpenRead. – xzczd Nov 24 '18 at 7:21
• @ABCDEMMM It should be read@str. If you still don't understand, remove all the ; and observe those outputs, and think about what input should be used for read, and finally, don't forget to check the document by pressing F1. – xzczd May 2 '19 at 3:49