removing migschema defs from resource mate xml
[migration-tools.git] / text / join_lines_if_short
1 #!/usr/bin/perl -w
2 ###############################################################################
3 =pod
4
5 =head1 NAME
6
7 join_lines_if_short - program for joining some adjacent lines from stdin or one
8 or more files
9
10 =head1 SYNOPSIS
11
12 B<join_lines_if_short> [options...] [text files...]
13
14 =head1 DESCRIPTION
15
16 B<join_lines_if_short> is used to join adjacent lines from stdin or one or more
17 files, trimming trailing line feeds and carriage returns, and sending the output
18 to stdout.  Lines are only joined if the first line is short the expected number
19 of columns.
20
21 =head1 OVERVIEW
22
23 B<join_lines_if_short> understands the following optional arguments:
24
25 =over 15
26
27 =item --help
28
29 Show this documentation.
30
31 =item --delimiter=<d>
32
33 B<join_lines_if_short> will use the specified delimiter <d> for determining the
34 column count for each line.  The default is to assume tab as the delimiter.
35
36 =item --join_delimiter=<d>
37
38 B<join_lines_if_short> will use the specified delimiter <d> when joining lines.
39 The default is to use no delimiter.
40
41 =item --columns=<n>
42
43 B<join_lines_if_short> will expect each line to contain <n> columns.  If a line
44 has fewer than <n> columns, then this is the trigger for joining that line with
45 the next line.  The new line will be reconsidered and potentially joined with
46 the next line and so on.
47
48 =back
49
50 =cut
51 ###############################################################################
52
53 use strict;
54 use Pod::Usage;
55 use Getopt::Long;
56
57 my $help;
58 my $delimiter = "\t";
59 my $join_delimiter = "";
60 my $colcount;
61
62 GetOptions(
63         'delimiter=s' => \$delimiter,
64         'join_delimiter=s' => \$join_delimiter,
65         'columns=s' => \$colcount,
66         'help|?' => \$help
67 );
68 pod2usage(-verbose => 2) if $help || ! defined $colcount; 
69
70 my $line_buffer = '';
71 while (my $line = <>) {
72     chomp $line;
73     if ($line_buffer eq '') {
74         $line_buffer = $line;
75     } else {
76         $line_buffer = "$line_buffer$join_delimiter$line";
77     }
78     my @f = split /$delimiter/, $line_buffer;
79     if (scalar(@f) >= $colcount) {
80         $line_buffer =~ s/\x0D//g; # strip embedded carriage returns
81         print "$line_buffer\n";
82         $line_buffer = '';
83     }
84 }
85 if ($line_buffer ne '') {
86     print "$line_buffer\n";
87 }