File: get_line_no

package info (click to toggle)
dballe 4.0.2-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 13,528 kB
  • ctags: 3,612
  • sloc: ansic: 20,640; cpp: 10,131; sh: 9,128; python: 5,459; ruby: 747; makefile: 691; perl: 629; f90: 339; fortran: 139
file content (36 lines) | stat: -rwxr-xr-x 517 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/perl -w

# Convert a file offset into a line number

use strict;
use warnings;

if (!@ARGV)
{
	print qq{Usage: $0 file offset
Prints the line number of the given offset in the file.
};
	exit 0;
}

my $file = $ARGV[0];
my $num = $ARGV[1];

open(IN, $file) or die "Cannot open $file: $!";

my $ofs = 0;
while (<>)
{
	my $len = length($_);
	if ($ofs + $len > $num)
	{
		printf "%d:%d\n", $., $num - $ofs;
		exit 0;
	}
	$ofs += $len;
}

close(IN);

print STDERR "Offset is past the end of the file\n";
exit 1;