PERL - getting last file using wild cards


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL - getting last file using wild cards
# 1  
Old 03-18-2016
PERL - getting last file using wild cards

Hi experts,
I am new to perl.
I am trying to get the last file from set of files.
Using the below code; but getting error
pls help

Files:
Code:
-rw-r--r--   1 abc    abc      12584 Mar 18 16:22 /abc/def/ghi/xyz.HOSTNAME.2016.03.18.16.21.69709-6
-rw-r--r--   1 abc    abc      12623 Mar 18 16:25 /abc/def/ghi/xyz.HOSTNAME.2016.03.18.16.24.69709-7
-rw-r--r--   1 abc    abc      12623 Mar 18 16:27 /abc/def/ghi/xyz.HOSTNAME.2016.03.18.16.26.69709-8

Code:
Code:
$file = `ls -1rt /abc/def/ghi/xyz.${`hostname`}.2???.??.??.??.??.* | tail -1` ;
chomp($outfile = $file) ;
 print "THE OUTFILE IS ......................... $outfile\n";

ERROR:
Code:
/abc/def/ghi/xyz.HOSTNAME: No such file or directory
sh: line 2: .2???.??.??.??.??.*: not found
THE OUTFILE IS .........................

# 2  
Old 03-18-2016
try:
Code:
$file = `ls -1rt /abc/def/ghi/xyz.\$(hostname).2???.??.??.??.??.* | tail -1` ;
chomp($outfile = $file) ;
 print "THE OUTFILE IS ......................... $outfile\n";

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 03-19-2016
Code:
my $pattern = "/abc/def/ghi/xyz.$ENV{HOSTNAME}.2???.??.??.??.??.*";
my ($file) = qx{ls -t $pattern};
print "THE OUTFILE IS ......................... $file";

This User Gave Thanks to Aia For This Post:
# 4  
Old 03-21-2016
A more perl-ish way:

Code:
my @files = glob "/abc/def/ghi/xyz.HOSTNAME.2016*";
my @sort_files = sort { -M $a <=> -M $b } @files;

print "The OUTFILE is ... $sort_files[-1]\n";

# 5  
Old 03-21-2016
Quote:
Originally Posted by balajesuri
A more perl-ish way:

Code:
my @files = glob "/abc/def/ghi/xyz.HOSTNAME.2016*";
my @sort_files = sort { -M $a <=> -M $b } @files;

print "The OUTFILE is ... $sort_files[-1]\n";

Wouldn't be $sort_files[0]?

Code:
my ($files) = sort {-M $a <=> -M $b } glob "/abc/def/ghi/xyz.$ENV{HOSTNAME}.2???.??.??.??.??.*";
print "THE OUTFILE IS ......................... $file";


Last edited by Aia; 03-21-2016 at 11:55 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh - Checking directory trees containing wild cards

Hi Can somebody please show me how to check from within a KSH script if a directory exists on that same host when parts of the directory tree are unknown? If these wildcard dirs were the only dirs at that level then ... RETCODE=$(ls -l /u01/app/oracle/local/*/* | grep target_dir) ... will... (4 Replies)
Discussion started by: user052009
4 Replies

2. UNIX for Dummies Questions & Answers

How to use wild cards to find files beginning with upper and lower case

Im trying to use wild cards to find files that start with either an upper or lower case letter e.g. list files that beginning with b or B, i also want to sort them by the time they were last modified. e.g latest file created first. At the moment i have the following code that ls -d... (3 Replies)
Discussion started by: parker4001
3 Replies

3. Shell Programming and Scripting

Passing variable and wild card character to grep in Perl

HI All, I have a script that needs to find out a list of files in a directory, i pass the search parameter as an argument. opendir ( DIR, $dir ) || die "Error in opening dir $dirname\n"; @filename1 = (grep {/$File_pattern/ } readdir(DIR)); The problem is my file patterns are like... (1 Reply)
Discussion started by: amit1_x
1 Replies

4. UNIX for Dummies Questions & Answers

Renaming a file using wild characters

Hi, I have a file named as StateDCH_CSHCS_100131.txt and the digits in the file name changes everyday. I have to read this file from UTL_FILE package of Oracle 10g. So I want to rename this file picking StateDCH*.txt to StateDCH_Standard.txt a fixed name so that my UTL_FILE utility can be able... (4 Replies)
Discussion started by: master346
4 Replies

5. Linux

problem of wild cards matching..$ls t[A-Z] output

hi all, i have created a file by name tt and while listing the file ,i used the command $ls t from which i was expecting not to list tt file output coz of uppercase range, But it listed that file tt file and am not able understand the reason. kindly help. NOTE OS :Red Hat Enterprise... (0 Replies)
Discussion started by: rajp_8007
0 Replies

6. Shell Programming and Scripting

Shuffling 'cards' using perl.

I would be very grateful if someone could help me with this; using pop, shift, push, and the starting code below, I'd like a script that sufficiently "shuffles" a simulated deck of cards before printing the top five cards. #!/usr/bin/perl @startingdeck = ("A H","2 H","3 H","4 H","5 H","6... (9 Replies)
Discussion started by: DemonixX
9 Replies

7. UNIX for Dummies Questions & Answers

wild char * in if

if ; then => is it correct? i need to check 10 files size and do the same action. The file name is same but extension of the files are different. how do i deal with it? Please help (1 Reply)
Discussion started by: nidhink
1 Replies

8. Shell Programming and Scripting

Find Existence of File with wild card using Csh

Hi All, I would like to find out the existence of files with wild card using CSH. I have used the below code but does not seem to work. Can any expert give me some advice ? set nonomatch set pattern = "_xxx" set filetype = ( *$pattern* ) if ( -e $filetype) then echo... (2 Replies)
Discussion started by: Raynon
2 Replies

9. Shell Programming and Scripting

Need to put a space between wild cards..

how do you put a space in a wild card thing.. example "LastAnalysisT.*${MONTH}.*${DAY}.*20${YEAR}" I need to make it something like "LastAnalysisT.*${MONTH}<I need a space here>.*${DAY}.*20${YEAR}" So it would be something like: LastAnalyst*May 24*2004 Where * just means I dont... (1 Reply)
Discussion started by: LordJezo
1 Replies
Login or Register to Ask a Question