I'm trying to split a large text files into several text files. I found another thread from a few years ago with a very similar premise but couldn't find my exact situation.
https://unix.stackexchange.com/a/64691/183674
How would I split the following data if the first line didn't start with 00:00:00:00?
00:00:00:00 00:00:05:00 01SC_001.jpg
00:00:14:29 00:00:19:29 01SC_002.jpg
00:01:07:20 00:01:12:20 01SC_003.jpg
00:00:00:00 00:00:03:25 02MI_001.jpg
00:00:03:25 00:00:08:25 02MI_002.jpg
00:00:35:27 00:00:40:27 02MI_003.jpg
00:00:00:00 00:00:05:00 03Bi_001.jpg
00:00:05:19 00:00:10:19 03Bi_002.jpg
00:01:11:17 00:01:16:17 03Bi_003.jpg
00:00:00:00 00:00:05:00 04CG_001.jpg
00:00:11:03 00:00:16:03 04CG_002.jpg
00:01:12:25 00:01:17:25 04CG_003.jpg
Here's the code for reference:
#!/usr/bin/env perl
use strict;
use warnings;
open(my $infh, '<', 'ABC_TabDelim.txt') or die $!;
my $outfh;
my $filecount = 0;
while ( my $line = <$infh> ) {
if ( $line =~ /^00:00:00:00/ ) {
close($outfh) if $outfh;
open($outfh, '>', sprintf('ABC%02d_TabDelim.txt', ++$filecount)) or die $!;
}
print {$outfh} $line or die "Failed to write to file: $!";
}
close($outfh);
close($infh);
I tried adding a print $line; in the next line after the while statement to attempt to make it read line by line as shown in other tutorials but this did not rectify the issue.
I would appreciate any input.
edit: So for an example like
00:01:16:17 00:00:05:00 01SC_001.jpg
00:00:14:29 00:00:19:29 01SC_002.jpg
00:01:07:20 00:01:12:20 01SC_003.jpg
00:00:00:00 00:00:03:25 02MI_001.jpg
00:00:03:25 00:00:08:25 02MI_002.jpg
00:00:35:27 00:00:40:27 02MI_003.jpg
00:00:00:00 00:00:05:00 03Bi_001.jpg
00:00:05:19 00:00:10:19 03Bi_002.jpg
00:01:11:17 00:01:16:17 03Bi_003.jpg
00:00:00:00 00:00:05:00 04CG_001.jpg
00:00:11:03 00:00:16:03 04CG_002.jpg
00:01:12:25 00:01:17:25 04CG_003.jpg
I would like to get three seperate files, respectively containing
00:00:00:00 00:00:03:25 02MI_001.jpg
00:00:03:25 00:00:08:25 02MI_002.jpg
00:00:35:27 00:00:40:27 02MI_003.jpg
00:00:00:00 00:00:05:00 03Bi_001.jpg
00:00:05:19 00:00:10:19 03Bi_002.jpg
00:01:11:17 00:01:16:17 03Bi_003.jpg
00:00:00:00 00:00:05:00 04CG_001.jpg
00:00:11:03 00:00:16:03 04CG_002.jpg
00:01:12:25 00:01:17:25 04CG_003.jpg
discarding the first three lines.
00:00:00:00in the first column of the first row, for example).