# Looking for specific data in .txt file

I've the following code:

Import["test.txt"]


And it gives me:

1,10

2,20

3,30

4,40

5,50

6,60

7,70

8,80

9,90

10,100

11,110

12,120

13,130

14,140

15,150

16,160

17,170

18,180

19,190

But I only want the time values $8,9,10$ how can I improve my code so that only those times are printed in my notebook?

I used this code but that does not work, but why?

s = Import["test.txt"];
data = ToExpression@StringSplit[
Rest[
StringSplit[s, {""}]
],
","];
Select[data, MemberQ[{8, 9, 10}, #[[1]]] &]


The following seems to work:

data = Table[{i, i*10}, {i, 1, 19}];


which mimics what final form of the data should look like

Select[data, MemberQ[{8, 9, 10}, #[[1]]] &]
(*{{8, 80}, {9, 90}, {10, 100}}*)


I am not certain if the contents of your test file are as you posted or there are just typos. But using ImportString[] to sit in for Import[] for immediate results

    data2 = ImportString["1,10
2,20
3,30
4,40
5,50
6,60
7,70
8,80
9,90
10,100
11,110
12,120
13,130
14,140
15,150
16,160
17,170
18,180
19,190", "CSV"];
Select[data2, MemberQ[{8, 9, 10}, #[[1]]] &]
(*{{8, 80}, {9, 90}, {10, 100}}*)


So Try

data = Import["test.txt", "CSV"]

• Yes, I was just about to suggest the Import["test.txt", "CSV"] command – KraZug May 4 '18 at 8:44
• @KraZug The problem with this is, that the .txt file in reality much bigger is. So I've soooo much measured points that I want to stick with my code but I think there is problem in my code in the data = ToExpression@ StringSplit[Rest[StringSplit[s, {"(", ")(", ")"}]], ","] part. – Looper May 4 '18 at 8:45
• @Hans See my reaction to KraZug. – Looper May 4 '18 at 8:45
• @Looper, you mean that you have lots of other bits of text in the .txt file? – KraZug May 4 '18 at 8:48
• @KraZug Yes indeed, I asked the question here with a lower amount of points because if my code works it also works with a lot of data point. – Looper May 4 '18 at 8:49

If you take the code you have given and evaluate it one part at a time, starting from the inside, and you'll find it doesn't do what you want it to (it starts by splits on every character and then drops the first character for some reason).

selectNumberLines[textData_, num_] :=  Pick[StringSplit[textData, "\n"],
StringContainsQ[StringSplit[textData, "\n"], StartOfLine ~~ ToString[num] ~~ "," ~~ ___]]


It splits the block of text from the Input into individual lines before finding those that start with the number that you give.

selectNumberLines[s, #] & /@ {8, 9, 10}
(* {{"8,80"}, {"9,90"}, {"10,100"}}  *)


If your data is always of the form "number, stuff you want", you could try this to get only the later parts:

selectNumberLinesValues[textData_, num_] :=
ToExpression@Last@Last@StringSplit[selectNumberLines[textData,num], ","]

selectNumberLinesValues[s, #] & /@ {8, 9, 10}
(* {80, 90, 100} *)


But you need to be careful with matching patterns like this.

• The problem with this is, that I only want to select on the first number (that is the time) so when I want to extract $8,9$ and $10$ I only knows those times and not the $80,90$ and $100$ part. – Looper May 4 '18 at 9:13
• @Looper, that code only takes the first value and returns the second part. – KraZug May 4 '18 at 12:33