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 large text file with three columns. I'm trying to write a PERL script that splits the file up based on the value of the 3rd column. So every time the third column reads 0, a new file is created and all the data up until the next 0 is found is written to that new file. This should happen over and over until the initial file has been entirely split up.
May I suggest creating filenames whose numeric indexes are padded with enough leading zeros that they sort equivalently both alphabetically and numerically?
This is my scripts so far. What seems to happen though, is all the data simply gets rewritten into the new file.
#!/usr/bin/perl
my $chr = 1;
my $Input = "data4.txt";
my $Output= "data_$chr.txt";
open (Data,"<$Input");
open (NData,">$Output");
foreach $line(<Data>){
($a, $b, $c) = split/\t/,$line;
if ($c eq 0) {
$chr++;
close NData;
open (NData,">$Output");
}
print NData ($line);
}
}
#!/usr/bin/perl -w
use strict;
my $chr = 1;
my $Input = "data4.txt";
my $Output= "data_$chr.txt";
open (Data,"<$Input");
open (NData,">$Output");
foreach $line(<Data>){
($a, $b, $c) = split/\t/,$line;
if ($c eq 0) {
$chr++;
close NData;
$Output= "data_$chr.txt";
open (NData,">$Output");
}
print NData $line;
}
And of course, there's always that perennial, pop favorite "split"
Won't necessarily work the way you want ... but might actually work a lot better
Just a thought...
Split won't be any good if the input isn't always split on the same
interval, which is what his sample data suggests; the criteria is
of the "0 0 0" kind, not "split at every 5th line".
"eq" is used to compare strings, another alternative is to chomp $c and compare with c$ eq "0".
since the first line matches the if, it will then immediately close the first file(data_1.txt) with 0 bytes, but it shouldn't be much drama to exclude it with a condition.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.