The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need Help w/PERL system command lorik Shell Programming and Scripting 1 12-06-2006 08:09 PM
Perl system ssh find question thumper Shell Programming and Scripting 3 07-19-2006 04:41 PM
Perl - backticks v system in if statements gjkeenan Shell Programming and Scripting 5 07-31-2005 06:41 AM
Which version of perl is installed on my system? kfad Shell Programming and Scripting 6 04-20-2005 09:44 PM
Perl run system command gdboling Shell Programming and Scripting 1 09-02-2003 10:30 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-22-2005
reggiej reggiej is offline
Registered User
  
 

Join Date: May 2005
Posts: 27
perl - system command

Can this be done without using te system command? I have a directory with a large number of files in it, but I am interested in only the 8 most recent.

The directory looks like
-rw-rw-rw- 1 adsm adsm 13412 Sep 22 08:31 events_dump_09222005.csv.gz
-rw-rw-rw- 1 adsm adsm 13405 Sep 21 08:31 events_dump_09212005.csv.gz
-rw-rw-rw- 1 adsm adsm 13237 Sep 20 08:31

I am getting the date out of the file to be used in another module. What I have works, but want I want to know is if it can be done without resorting to temporary file (listfiles) and the system command

#!/usr/bin/perl

system( 'ls -tr /adsm/DATASRC/events/ | tail -8r > /adsm/SCRIPTS/BKUPINFO/listfiles');
open (LSTFILES, "</adsm/SCRIPTS/BKUPINFO/listfiles");
while (<LSTFILES>) {
print($_);
($pt1,$pt2) = split(/_dump_/, $_ );
chomp($pt2);
print "$pt2\n";
($pt3,$pt4) = split(/\./,$pt2);
print "$pt3\n";
}



close(LSTFILES);
  #2 (permalink)  
Old 09-22-2005
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
Yes, in fact I won't resort to shell equivalents if something can be solely done from within Perl, because this ensures the highest portability. The following will give you a list of top 8 files with the latest mtime in descending order of mtime:

Code:
#!/usr/bin/perl

my $base = "/adsm/DATASRC/events/";

opendir DIR, $base or die "Cannot read dir!";
my @list = readdir DIR;
closedir DIR;
my @list_with_mtime = map { ($_ !~ /^\./?[$_, (stat($base . $_))[9]]:()) } @list;
my @r_sorted_list = map { $$_[0] } sort { $$b[1] <=> $$a[1] } @list_with_mtime;

# Show top 8
{
	local $, = "\n";
	print @r_sorted_list[0..7];
}
  #3 (permalink)  
Old 09-23-2005
reggiej reggiej is offline
Registered User
  
 

Join Date: May 2005
Posts: 27
Thanks for the reply. It absolutely does what I am looking for.
Now I need to understand it. My understanding of the map function is that it works like a foreach loop. Is that right?

Can you explain this statement map { ($_ !~ /^\./?[$_, (stat($base . $_))[9]])) } @list
  #4 (permalink)  
Old 09-23-2005
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
It looks complex, but it is actually quite simple if I put it in a nicer looking form.

Code:
map {
     (     
           $_ !~ /^\./?
                 [
                       $_, 
                       (stat($base . $_))[9]
                 ]:
                 ()
    ) 
} @list
So we read the list of file (including directory entries) names in @list. For each name read, we carry out the following operation:
  • If the filename does not start with a ".", it is not a hidden file. We will put the filename together with its mtime as an array reference to facilitate sorting later on.
  • Otherwise, a hidden file is ignored by yielding an empty array.

It will pass through every entry of @list and collect the evaluated value in the map{} block and return the aggregated list. At the end of this statement we will get an array of array references containing the filename and the corresponding mtime. Entries that yield an empty array are ignored because an empty array upon interpolated in a list will "dissolve" itself. The net effect in this piece of code is that hidden files will disappear from the resulting, aggregated list. This is what we want if we want to ignore all hidden files.

i.e. ("a", "b", (), "c") will give ("a", "b", "c") so this is how this trick works.

Yes, the next statement sorts this intermediate list in descending order of mtime, and then reconstruct an array with filenames only (by cleaning the temporary mtime values and removing the references).
  #5 (permalink)  
Old 09-26-2005
reggiej reggiej is offline
Registered User
  
 

Join Date: May 2005
Posts: 27
Thank you for the reply.

I am also trying to understand the sort statement { $$_[0] } sort { $$b[1] <=> $$a[1] . I understant what it does but what I am not familiar with is the $$.
  #6 (permalink)  
Old 09-26-2005
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
As I said, I have made some temporary references with the first statement. In my case this is an array reference (think about it like a pointer to an array) containing the filename together with the mtime as two separate array items.

To access an item from a normal array by index you will

$array[0], $array[1]

If the corresponding array is pointed to by a reference and you need to access an item through the reference, that becomes

$$arrayref[0], $$arrayref[1]

OR (both syntax will work)

$arrayref->[0], $arrayref->[1]

which literally means "dereference and then index in the underlying array".

Understanding these require some knowledge about references in Perl.

The official page that talks about this:
http://perldoc.perl.org/perlref.html
Closed Thread

Bookmarks

Tags
mtime

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:23 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0