# Import a file only if it contains a certain pattern

Let's say I have three files containing:

File 1:
x y z
1 4 6

File 2:
x y z
2 5 9

File 3:
x y z
5 4 8


My goal is to import the data from a file only if in this file y == 4 (so here I would want to import files 1 and 3).

Is there a way to do that with functional programming? The only solution I can think of currently is to loop through all the files, check if data[[2, 2]] == 4, and append this data to a list if true. Is there a better way to do that?

And if I have to loop, is it better to import all the files as lists and then discard the lists that do not match y == 4? Or is it better to import only the files matching y == 4 in the first place?

-
How do you check if y==4 without importing the files somehow ? –  b.gatessucks Jan 8 '13 at 10:48
ok "importing" maybe was not the right word. Let's say I want to keep those files only, and I'm wondering if there is a function with an option allowing me to do something like Import[file, only if y == 4], and if not, if from a list with the data from all files, I can know without looping what files contain y == 4. –  Sulli Jan 8 '13 at 10:58
In Mathematica 9 ImageFileApply and ImageFileScan "can be used to read, process, and write successive blocks of data from a very large image file whose size could exceed available memory", i.e. to process without excessive loading. –  Ernst Stelzer Jan 8 '13 at 14:46
If its a large file with tag info at the beginning you might want the do raw open/read operations to get the key you need, then import only as needed. For small files my guess is you are just as well off to import the whole thing and descard what yuo dont need. –  george2079 Jan 8 '13 at 16:46

Select[Select[FileNames["file*.txt"],Length[FindList[#,{"y","4"},2]]===2&],