File: sort_file

package info (click to toggle)
medicalterms 20160103-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 528 kB
  • ctags: 33
  • sloc: sh: 472; perl: 197; makefile: 91
file content (94 lines) | stat: -rwxr-xr-x 2,298 bytes parent folder | download | duplicates (3)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env perl
#
# Script for sorting all dictionary words in the specified file,
# without messing with the GNU license comment at the beginning
#
# Copyright © 2005-2016 Dr. Tobias Quathamer <t.quathamer@mailbox.org>
#
# This file is part of Medicalterms.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

use warnings;
use strict;

use locale;
use POSIX;
POSIX::setlocale(LC_COLLATE, 'de_DE.UTF-8');

use Getopt::Long;

sub version
{
	my $filename = substr($0, rindex($0, "/") + 1);
	print "$filename version 0.1\n";
	print "Copyright © 2005-2016 Dr. Tobias Quathamer <t.quathamer\@mailbox.org>\n";
}

sub usage
{
	my $filename = substr($0, rindex($0, "/") + 1);
	&version;
	print "\nThis utility sorts all dictionary words in the specified file,\n";
	print "without messing with the GNU license comment at the beginning and\n";
	print "writes the result to standard output.\n\n";
	print "Usage: $filename [file.txt]\n";
}

my $help;
my $version;
my $options = GetOptions(
	"help" => \$help,
	"version" => \$version)
	or exit;

if ($version) {
	&version;
	exit;
};
if ($help) {
	&usage;
	exit;
};

sub without_suffixes
{
	my @stem_a = split /\//,  $a;
	my @stem_b = split /\//,  $b;
	(defined($stem_a[0]) ? $stem_a[0] : 0) cmp (defined($stem_b[0]) ? $stem_b[0] : 0);
}

# the start of the actual program
foreach my $file (@ARGV) {
	my $gnu_header = "";
	my @file_contents = "";

	open FILE, "< $file"
		or die "Could not open $file: $!\n";
	while (<FILE>) {
		if (/^\s*#/) {
			# extract the GNU license header
			$gnu_header .= $_;
		} else {
			# store the word list in an array
			push(@file_contents, $_);
		}
	}

	print $gnu_header;
	print sort without_suffixes @file_contents;

	close FILE;
}