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