Perl : printing the files in the exact order as it is..


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl : printing the files in the exact order as it is..
# 1  
Old 03-28-2013
Perl : printing the files in the exact order as it is..

Hi folks,

I am trying to open directory and then print the files in that directory in PERL.
I have used the below code.
Code:
opendir(DIR,$destination) or die ("Cannot open directory $destination");
my @bwfiles = readdir(DIR);
print @bwfiles;

I am able to retrieve all the files the files are not printed according to the timestamp.

In directory files are listed as below..
Code:
-rw-r--r-- 1 johng users 10471630 2013-03-19 12:56 file1.txt
-rw-r--r-- 1 johng users    45496 2013-03-19 13:37 file2.txt
-rw-r--r-- 1 johng users    45512 2013-03-24 13:42 file3.txt
-rw-r--r-- 1 johng users        0 2013-03-28 06:17 file4.txt
-rw-r--r-- 1 johng users        0 2013-03-28 06:18 file5.txt

But while printing the files..
Code:
file5.txt
file3.txt
file4.txt
file1.txt
file2.txt

But I expected is
Code:
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt

How to print them in exact order in the directory..

Could anyone please help me on this ?
# 2  
Old 03-28-2013
Code:
#! /usr/bin/perl -w
use strict;

opendir DH, "/path/to/dir" || die;
my (%files, $f);
while ($f = readdir (DH)) {
    next if ($f eq "." || $f eq "..");
    $files{(stat($f))[9]} = $f;
}    
closedir DH;

print "$files{$_}\n" for (sort {$a <=> $b} keys %files)


Last edited by balajesuri; 03-28-2013 at 02:00 PM..
# 3  
Old 03-28-2013
There is nothing wrong here -- you simply cannot depend on readdir's order. That's filesystem-dependent. If you need it ordered, you must sort it yourself.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace exact word by perl

my inputfile: This is a test and only a test for production - prod. echo "This is a test and only a test for production - prod" | perl -pi -e 's/prod/test/g' outputfile: This is a test and only a test for testuction - test I only want to replace prod, not production. I also... (3 Replies)
Discussion started by: loktamann
3 Replies

2. Shell Programming and Scripting

Grep -w not printing exact matches

Dear All, Here is my input TAACGCACTTGCGGCCCCGGGATAAAAAAAAAAAAAAAAAAAAATGGATT NAGAGGGACGGCCGGGGGCATAAAAAAAAAAAAAAAAAAAAAGGGATTTC NGGGTTTTAAGCAGGAGGTGTCAAAAAAAAAAAAAAAAAAAAAGGGATTT NTGGAACCTGGCGCTAGACCAAAAAAAAAAAAAAAAAAAATGGATTTTTG ATACTTACCTGGCAGGGGAGATACCATGATCAATAAAAAAAAAAAAAAAA... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

3. Shell Programming and Scripting

Grep or sed - printing line only with exact match

Hello. In my script, some command return : q | kernel-default | package | 3.19.0-1.1.g8a7d5f9 | x86_64 | openSUSE-13.2-Kernel_stable_standard | kernel-default | package | 3.19.0-1.1.g8a7d5f9 | i586 | openSUSE-13.2-Kernel_stable_standard | kernel-default ... (3 Replies)
Discussion started by: jcdole
3 Replies

4. Shell Programming and Scripting

Perl : not getting the exact output

I have written a perl code to read alphabet from console and prints the status. But every time it is printing "Entered is vowel". print "Enter any alphabet: "; $a = <STDIN>; if ( $a == "a" || $a == "e" || $a == "i" || $a == "o" || $a == "u" ) { print "entered is vowel"; } else... (4 Replies)
Discussion started by: scriptscript
4 Replies

5. Shell Programming and Scripting

Help printing files in ascending order of the fi le size (in bytes)

Hey guys I'm new to unix and need help printing files in a specified directory according to size in bytes as well as files with equal bites in alphabetical order the part i have done so far prints out all files in the directory as well as setting a time limit in which they have been modified ... (2 Replies)
Discussion started by: wessy
2 Replies

6. UNIX for Dummies Questions & Answers

printing fields in reverse order

command/script(apart from awk) to print the fields in reverse order that is last field has to come first and so on and first field has to go last Input store-id date sale ............. ............. ... (3 Replies)
Discussion started by: tsurendra
3 Replies

7. Shell Programming and Scripting

Perl, searching multiple files and printing returned line to new file

I am trying to find a way to utilise the full potential of my cpu cores and memory on my windows machine. Now, I am quite familiar with grep, however, running a Unix based OS is not an option right now. Unfortunately, the 32 bit grep for windows that I am running, I cannot run multiple... (1 Reply)
Discussion started by: Moloch
1 Replies

8. Shell Programming and Scripting

exact match in Perl

Hi By using select clause I'm trying to pull out the rows to a variable. If the variable has 0 row(s) selected then i'm printing some text message else printing some other text message if($xyz =~ m/0 row/) { print "0 rows "; } else { print " There are rows"; } By my problem... (4 Replies)
Discussion started by: pdreddy34
4 Replies

9. Shell Programming and Scripting

perl exact match

How to emulate grep -o option in perl. I mean to print not all line, only the exact match. echo "2A2 BB" | perl -ne 'print if /2A2/' 2A2 BB I want to print only 2A2. (2 Replies)
Discussion started by: mirusnet
2 Replies

10. Shell Programming and Scripting

AWK - printing certain fields when field order changes in data file

I'm hoping someone can help me on this. I have a data file that greatly simplified might look like this: sec;src;dst;proto 421;10.10.10.1;10.10.10.2;tcp 426;10.10.10.3;10.10.10.4;udp 442;10.10.10.5;10.10.10.6;tcp sec;src;fac;dst;proto 521;10.10.10.1;ab;10.10.10.2;tcp... (3 Replies)
Discussion started by: eric4
3 Replies
Login or Register to Ask a Question