<?xml version="1.0" encoding="windows-1252"?>
<node id="448418" title="UN*X ls, sed, cat... in Perl" created="2005-04-16 02:48:53" updated="2005-08-11 07:32:37">
<type id="1042">
CUFP</type>
<author id="404137">
ady</author>
<data>
<field name="doctext">
Dear Monks,&lt;br&gt;
&lt;br&gt;
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).&lt;br&gt;
&lt;br&gt;
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 ?&lt;br&gt;
&lt;br&gt;
&lt;code&gt;
#___________________________________________________________
#1A: Read files sorted ascending on timestamp
@files = `ls -t -1 -r $dir`;  # strip ./../ &amp; 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} &lt;=&gt; $files{$b} } keys %files);

#___________________________________________________________
#2A: Strip $author} line
system "sed s/{author}.*//g &lt;$file &gt;${file}1..txt";	
($TRACE&gt;=2) and system "cat ${file}1.txt";
	
#2B: Same using plain vanilla Perl ...
open (FH,  "&lt;${file}.txt")  or die "Can't open ${file}.txt: $!\n";
open (FH1, "&gt;${file}1.txt") or die "Can't create ${file}1.txt: $!\n";
while (&lt;FH&gt;) {	
   next if (/{author}/); 
   print FH1; ($TRACE&gt;=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&gt;=2) and system "cat VSort.txt";
	
#3B: Same using plain vanilla Perl ...
if ($TRACE&gt;=2) {
   open (FH, "&lt;$file") or die "Can't open $file: $!\n";
   while (&lt;FH&gt;) { print; }
   close(FH) or die "Can't close $file: $!\n";
}
&lt;/code&gt;
&lt;br&gt;
&lt;br&gt;
Best regards,&lt;br&gt;
   Allan Dystrup
&lt;br&gt;&lt;br&gt;
============================================================
&lt;div class="pmsig"&gt;&lt;div class="pmsig-404137"&gt;
As the eternal tranquility of Truth reveals itself to us, this very place is the Land of Lotuses&lt;br&gt;-- Hakuin Ekaku Zenji
&lt;/div&gt;&lt;/div&gt;</field>
</data>
</node>
