Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

UN*X ls, sed, cat... in Perl

by ady (Deacon)
on Apr 16, 2005 at 06:48 UTC ( [id://448418]=CUFP: print w/replies, xml ) Need Help??

Dear Monks,

Below some code, where i've (out of habit) used a couple of UN*X utilities to ease the job; Now i may have to deploy this code on MSWin PCs (without these utils, -- and i don't want to bundle freeware versions of the utils with the code).

So i've written some Perl alternatives for the "inlined" ls, sed and cat calls, but these are certainly not the only - and probably not the best - way to skin these cats. Any suggestions for improvement ?

#___________________________________________________________ #1A: Read files sorted ascending on timestamp @files = `ls -t -1 -r $dir`; # strip ./../ & process .msg/.txt #1B: Same using plain vanilla Perl ... foreach my $g qw(msg txt) { push @files, glob("$dir\\*.$g"); } my %files = map { $_, -M} @files; @files = reverse (sort { $files{$a} <=> $files{$b} } keys %files); #___________________________________________________________ #2A: Strip $author} line system "sed s/{author}.*//g <$file >${file}1..txt"; ($TRACE>=2) and system "cat ${file}1.txt"; #2B: Same using plain vanilla Perl ... open (FH, "<${file}.txt") or die "Can't open ${file}.txt: $!\n"; open (FH1, ">${file}1.txt") or die "Can't create ${file}1.txt: $!\n"; while (<FH>) { next if (/{author}/); print FH1; ($TRACE>=2) and print; } close (FH) or die "Can't close ${file}.txt: $!\n"; close (FH1) or die "Can't close ${file}1.txt: $!\n"; #___________________________________________________________ #3A: ($TRACE>=2) and system "cat VSort.txt"; #3B: Same using plain vanilla Perl ... if ($TRACE>=2) { open (FH, "<$file") or die "Can't open $file: $!\n"; while (<FH>) { print; } close(FH) or die "Can't close $file: $!\n"; }


Best regards,
Allan Dystrup

============================================================
As the eternal tranquility of Truth reveals itself to us, this very place is the Land of Lotuses
-- Hakuin Ekaku Zenji

Replies are listed 'Best First'.
Re: UN*X ls, sed, cat... in Perl
by dbwiz (Curate) on Apr 16, 2005 at 08:02 UTC
Re: UN*X ls, sed, cat... in Perl
by cog (Parson) on Apr 16, 2005 at 13:31 UTC
Re: UN*X ls, sed, cat... in Perl
by tlm (Prior) on Apr 16, 2005 at 14:55 UTC

    You won't be surprised to discover that someone already did (much of) this: ExtUtils::Command. Even if you don't want to make your code depend on ExtUtils::Command, you can crib from its source.

    the lowliest monk

Re: UN*X ls, sed, cat... in Perl
by graff (Chancellor) on Apr 17, 2005 at 04:58 UTC
    I was just curious why you would do this:
    @files = reverse (sort { $files{$a} <=> $files{$b} } keys %files);
    instead of this:
    @files = sort { $files{$b} <=> $files{$a} } keys %files;
    As for the "2A" example, you could simplify that to a single system call:
    my $redirect = ( $TRACE < 2 ) ? '>' : '| tee'; system( "sed s/{author}.*//g <$file $redirect ${file}1.txt";
    The trick there is that when you pipe to "tee filename", output is written to both stdout and to filename; and the stdout from "system()" is the same as the perl script's STDOUT, so you'll see it.
      Good questions...
      Your code for reversing the sort result list is clearly preferable.
      Your switch of redirection depending on trace level is also a nice twist.
      I've adopted both of your improvements here graff.
      Thanks! -- Allan
Re: UN*X ls, sed, cat... in Perl
by ady (Deacon) on Apr 16, 2005 at 14:25 UTC
    Thanks guys!
    There's "hours of fun for the whole family" here, in your links to Perl limerics, one-liners & tools!
    I've printed the docs. and look forward to spending some quality time toying with alternative ways of skinning the UN*X "cat",... and other animals.
    -- allan
Re: UN*X ls, sed, cat... in Perl
by mattr (Curate) on Apr 17, 2005 at 11:58 UTC
    An approach I took to writing backup code on windows systems was to use cygwin. Just the binary of the command (like bash.exe, cp.exe, etc.) and cygwin1.dll. After testing to confirm how they work when launched in windows, it just works, though contravenes your requirement not to bundle.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://448418]
Approved by dbwiz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-18 18:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found