-
Notifications
You must be signed in to change notification settings - Fork 4
/
show-newline.pl
executable file
·77 lines (66 loc) · 1.39 KB
/
show-newline.pl
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/perl
#
# Show line endings of a file (OA and/or 0D).
#
# Won't probably work with Mac line endings.
#
# See also dos-or-unix.pl
#
use warnings;
use strict;
use Getopt::Long;
my $nof_chars_shown = 10;
GetOptions (
'chars-shown=i' => \$nof_chars_shown,
'nof-lines=i' => \my $nof_lines);
if (@ARGV != 1) {
die "Specify file name";
}
my $file = shift;
die "$file does not exist" unless -e $file;
open (my $fh, '<', $file) or die;
binmode ($fh);
my $line_no = 1;
my $cur_line='';
my $c;
my $last_c = '';
while (read($fh, $c, 1)) {
last if $nof_lines and $line_no > $nof_lines;
if ($c eq "\x0d") {
if ($last_c eq "\x0a") {
end_of_new_line_sequence();
}
start_of_new_line_seqence();
printf (" 0D");
}
elsif ($c eq "\x0a") {
if ($last_c eq "\x0a") {
end_of_new_line_sequence();
}
if ($last_c ne "\x0d") {
start_of_new_line_seqence();
}
printf (" 0A");
}
else {
if ($last_c eq "\x0a" or $last_c eq "\x0d") {
end_of_new_line_sequence();
}
if (length($cur_line) < $nof_chars_shown) {
$cur_line .= $c;
}
}
$last_c = $c;
}
close $fh;
if ($last_c eq "\x0a") {
end_of_new_line_sequence();
}
sub start_of_new_line_seqence {
printf ("%4d| %-${nof_chars_shown}s", $line_no, $cur_line);
$cur_line = '';
}
sub end_of_new_line_sequence {
print "\n";
$line_no ++;
}