X-Git-Url: http://git.equinoxoli.org/?p=migration-tools.git;a=blobdiff_plain;f=text%2Fjoin_lines;fp=text%2Fjoin_lines;h=91c08e54ef2602a34c6e662392991c474b3c8e45;hp=0000000000000000000000000000000000000000;hb=f9201dc2d1699f5161e5e29690a1634e8063bb85;hpb=d1812fa8c4c9e220978d650adb3611c978a2a56b diff --git a/text/join_lines b/text/join_lines new file mode 100755 index 0000000..91c08e5 --- /dev/null +++ b/text/join_lines @@ -0,0 +1,100 @@ +#!/usr/bin/perl -w +############################################################################### +=pod + +=head1 NAME + +join_lines - program for joining adjacent lines from stdin or one or more files + +=head1 SYNOPSIS + +B [options...] [text files...] + +=head1 DESCRIPTION + +B is used to join adjacent lines from stdin or one or more files, +trimming trailing line feeds and carriage returns, and sending the output to +stdout. Arguments are used to define the criteria and exact behavior. + +=head1 OVERVIEW + +B understands the following optional arguments: + +=over 15 + +=item --help + +Show this documentation. + +=item --delimiter= + +B will use the specified delimiter to separate joined lines. The +default is to use no delmiter when joining lines. + +=item --lines= + +This will cause B to join every lines into one line. + +=item --record-separator= + +This will cause B to immediately start a new line when it encounters +a line solely containing . That line itself will not otherwise be used. This +argument may be used in conjunction with --lines, but probably should not be. :) + +=item --record-separator-empty-line + +This works like --record-separator, but is a way to define an "empty" line as +being the record separator. + +=back + +=cut +############################################################################### + +use strict; +use Pod::Usage; +use Getopt::Long; + +my $help; +my $delimiter; +my $linecount; +my $record_separator; +my $record_separator_empty_line; + +GetOptions( + 'delimiter=s' => \$delimiter, + 'lines=s' => \$linecount, + 'record-separator=s' => \$record_separator, + 'record-separator-empty-line' => \$record_separator_empty_line, + 'help|?' => \$help +); +pod2usage(-verbose => 2) if $help; + +my $count = 0; +my @lines = (); + +sub join_lines { + print join($delimiter || '', @lines) . "\n"; + @lines = (); + $count = 0; +} + +while (my $line = <>) { + $count++; + $line =~ s/[\r\n]+$//g; + if (defined $record_separator_empty_line && $line eq '') { + join_lines(); + } elsif (defined $record_separator && $line eq $record_separator) { + join_lines(); + } elsif (defined $linecount && $count == $linecount) { + push @lines, $line; + join_lines(); + } elsif (! defined $linecount && ! defined $record_separator && ! defined $record_separator_empty_line) { + print "$line\n"; # passthru when given no arguments + } else { + push @lines, $line; + } +} +if (scalar(@lines) > 0) { + print join($delimiter || '', @lines) . "\n"; +}