[SOLVED] how to extract a subset from a huge dataset
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a huge file which has 450G. Its format is as below
x1 50020 A 1
x1 50021 B 8
x1 50022 C 9
x1 50023 A 10
x2 50024 D 5
x2 50025 C 7
x2 50026 F 8
x2 50027 M 1
:
:
Now, I want to extract a subset from this file. In this subset, column 1 is x10, column 2 is from 600000 to 30000000. I wrote the following perl script but it doesn't work:
open (IN, $file1);
while ($line = <IN>)
{
chomp($line);
@array = split(/\t/,$line);
if ($array[0] eq 'x10')
{
if (($array[1] >= 600000) && ($array[1] <= 26279795))
{
open (OUT, ">>$file2");
print OUT "$line\n";
close OUT;
}
}
}
close IN;
exit;
I guess the input file and output file are both too big that my script can't handle it.
Anyone knows if there is any good way to do it? Perl or Shell scripts are preferred..
sed -n '/x10 600000/,/x10 30000000/p' inputFile > newFile
This should do it. I only used spaces in my regExp, so you will have to edit this. To make a tab in the shell press Ctrl-v and then tab. Alternativly you could type this line in a text editor where tab will work as expected.
P.S.: You might want to create a smaller file with the same format and test it. Going through 450G of data might take a bit too long for testing purposes.
awk is, as you have seen, a very powerful tool that is expressly designed for tasks such as these. Learn it. Use it well.
The Perl programming language actually grew directly out of this one. It has since taken on a life of its own, and most of that "life" these days comes out of a vast library of tested software that you can use with it.
There are many power-tools in the Unix/Linux environment, and, as the Perl folks would say:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.