file_columner.pl 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 18 August 2013
  5. # https://github.com/trizen
  6. # Put two or more files together as columns.
  7. use 5.010;
  8. use strict;
  9. use autodie;
  10. use warnings;
  11. use List::Util qw(any);
  12. use Getopt::Std qw(getopts);
  13. binmode(\*STDOUT, ':encoding(UTF-8)');
  14. my %opt = (s => 25);
  15. getopts('s:h', \%opt);
  16. sub usage {
  17. die <<"USAGE";
  18. usage: $0 [options] [files]
  19. options:
  20. -s <i> : number of spaces between columns (default: $opt{s})
  21. -h : print this message and exit
  22. Example: perl $0 -s 40 file1.txt file2.txt > output.txt
  23. USAGE
  24. }
  25. my @files = grep {
  26. -f or warn "`$_' is not a file!\n";
  27. -f _;
  28. } @ARGV;
  29. if ($opt{h} || !@files) {
  30. usage();
  31. }
  32. my @fhs = map {
  33. open my $fh, '<:encoding(UTF-8):crlf', $_;
  34. $fh;
  35. } @files;
  36. while (any { !eof($_) } @fhs) {
  37. printf "%-$opt{s}s " x $#fhs . "%s\n", map {
  38. chomp(
  39. my $line =
  40. eof($_)
  41. ? q{}
  42. : scalar(<$_>)
  43. );
  44. $line;
  45. } @fhs;
  46. }