# Import selected columns from a tab delimited text file

I have a very huge file (1GB) from which I desire to import only the 5th column. It is a tab delimited text file with a .txt extension. Can you please help me with this?

I generally use Import["filename.txt","Table"] to import my files, but now, I don't want the whole file, which takes a long time to load.

On Linux you could use this simple solution.

Linux Shell

From the shell:

$cat myfile.txt | cut -f 5 > col5.txt Then import into Mathematica as usual. data = Import["col5.txt","Table"]  ReadList This can be condensed to one ReadList command. data = ReadList["!cat myfile.txt | cut -f 5"];  • Or awk '{print$5}' myfile.txt :) – rm -rf Nov 6 '12 at 18:08
• @rm-rf Yes, there is a beauty in the simplicity of awk :) – image_doctor Nov 6 '12 at 18:14
• oh I didn't know we can incorporate linux commands in mathematica. I use cut command all the time. instead of cat and then piping it, we can directly use the cut command on the file as well. Thanks for your solution! – preeti Nov 6 '12 at 19:04
• Works perfectly on windows as well using sourceforge.net/projects/unxutils or by installing msysgit :) – Ajasja Nov 6 '12 at 19:21
• @image_doctor How can I use this solution if 'my file.txt' is given as ToFileName[...]? Also, how is the command for reading the first four columns only? – Valerio Dec 14 '13 at 14:32

You might try this:

Import["data.txt", {"Data", All, 5}]


If you want to load only the fifth column of the first two lines you can run this:

Import["data.txt", {"Data", {1, 2}, 5}]


Update

Some testing on a relatively small data file (12MB) might prove that this is not really the solution you're after. It seems that it takes much more time to load just one column (~130 seconds) than to load the entire file with Import["data.txt","Table"] (~4 seconds).

• yes, I tried this from mathematica documentation and had the time issue. That was the reason behind this post. I missed to mention that. – preeti Nov 6 '12 at 19:03
• ReadList is almost always faster then Import. – George Wolfe Nov 6 '12 at 21:24

If you are on a Unix-like system or have installed cut or awk on other systems letting these do the extraction is most probably the most efficient way to solve your problem. If for some reason you are looking for a Mathematica-only way here is one:

readColumn[filename_, colnum_, numcols_, numlinesperchunk_Integer: 500] :=
Module[{
res = {""}, data
},
data = Reap[
While[Length[res] > 0,
Sow[res =
]
][[2]];
Close[str];
Flatten[data]
]


to extract the 5th out of 6 columns you would use:

readColumn[filename,5,6]


using the 4th argument you can play around with numlinesperchunk for further optimization. Note that there is a tradeoff between maximal speed (very large values for numlinesperchunk) and minimal memory usage (low values for numlinesperchunk). For largish files I would expect a good value might be some hundreds of lines per chunk, but your milage may vary...

This might look a bit complicated for such a simple tasks but it tries to be efficient not only concerning runtimes but especially concerning memory which usually is a larger problem when importing large data files into Mathematica. Note that the code here is very similar to that for my answer to this question, where you find other ways to do similar things even more efficient at the price of more involved programming in Java and also some more background information...