I have a file with a bunch of lines. I have a list of the bytes offsets corresponding with the start of each line. I want each line that corresponds with the byte offset. Is there a way to do this in unix, perl or python? I have to do this at a much larger scale than described.
File:
abcd
bcde
cdef
Byte Offsets:
0
10
Desired Output:
abcd
cdef
with open(filename, 'r') as f:
for offset in offsets:
f.seek(offset)
print(f.readline())
References:
Quickie perl:
my @offsets = ( 0, 10 );
open (my $data, '<', 'file.txt') || die "Can't open input: $!\n";
foreach my $offset (@offsets)
{
seek( $data, $offset, 0 );
my $line = <$data>;
print $line;
}
close $data;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With