# 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.

-
Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2)Read the FAQs! 3) When you see good Q&A, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. ALSO, remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign – chris Nov 6 '12 at 20:39

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 :) – R. M. 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}]


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).