1#!/usr/bin/env perl
2#
3# sort-qif.pl -- sort QIF file
4#
5# Sort QIF file.  This assumes that the header lists in the QIF file have a
6# leading comment that looks like '# stream $number'.
7#
8# Usage: sort-qif.pl [--strip-comments] [files] [--output FILE]
9
10use Getopt::Long;
11
12GetOptions("strip-comments" => \$strip_comments, output => \$output);
13
14if (defined($output)) {
15    open STDOUT, ">", $output or die "cannot open $output for writing: $!";
16}
17
18$/ = "\n\n";
19
20@chunks = map $$_[1],
21          sort { $$a[0] <=> $$b[0] }
22          map { /^*#\s*stream\s+(\d+)/; [ $1, $_ ] }
23          <>;
24
25if ($strip_comments) {
26    for (@chunks) {
27        s/^#.*\n//mg;
28    }
29}
30
31print @chunks;
32