Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
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

2 Answers 2

up vote 5 down vote accepted

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"];
share|improve this answer
    
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
1  
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}]

You can find more about this here.

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

share|improve this answer
    
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
1  
ReadList is almost always faster then Import. –  George Wolfe Nov 6 '12 at 21:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.