#!/usr/bin/perl # The Missing Textutils, Ondrej Bojar, obo@cuni.cz # http://www.cuni.cz/~obo/textutils # # 'autocat' is a unified replacement for 'cat', 'zcat', 'bzcat'. # It can also cat files given in a filelist or a glob (wildcard expression), # which is useful if the glob would expand to excessive number of arguments in # your shell. # # $Id: autocat,v 1.1 2010-11-02 16:07:41 bojar Exp $ use strict; use Getopt::Long; my $fl = undef; my @glob = (); my $sort = 0; my $prefix = undef; GetOptions( "filelist=s" => \$fl, "glob=s" => \@glob, "prefix=s" => \$prefix, # prefix each file with this line "sort" => \$sort, # sort the file list before catting ) or exit 1; if (defined $prefix) { $prefix =~ s/\\n/\n/g; $prefix =~ s/\\t/\t/g; } my @fl = (); if (defined $fl) { if ($fl eq "-") { *IN = *STDIN; } else { open IN, $fl or die "Can't read $fl"; } while () { chomp; push @fl, $_; } if ($fl ne "-") { close(IN); } } my @all = (@ARGV, (map {(glob($_));} @glob), @fl); my @use; if ($sort) { @use = sort {$a cmp $b} @all; } else { @use = @all } foreach my $f (@use) { print $prefix."\n" if defined $prefix; runcat($f); } if (0 == scalar @use) { # just follow stdin print while <>; } sub runcat { my $f = shift; my $usef = $f; my $cmd; if ( -e $f ) { my $ft = `file $f`; # file might not recognize some files! if ($f =~ /\.gz$/ || $ft =~ /gzip compressed data/) { $cmd = "zcat"; } elsif ($f =~ /\.bz2$/ || $ft =~ /bzip2 compressed data/) { $cmd = "bzcat"; } else { $cmd = "cat"; } } elsif ( -e ($usef = $f.".gz") ) { $cmd = "zcat"; } elsif ( -e ($usef = $f.".bz2") ) { $cmd = "bzcat"; } else { die "Not found: $f"; } system($cmd, $usef); }