Fetch the latest filename shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fetch the latest filename shell script
# 1  
Old 01-28-2016
Fetch the latest filename shell script

Hi

I want to fetch the latest file form the list
example


example= filename RATE_STATE_SETUPS.20151222.ccyymmdd.hhmmss.txt
File pick which have latest ccyymmdd.hhmmss


list of file in directory are
Code:
RATE_STATE_SETUPS.20151222.20151222.170101.txt
RATE_STATE_SETUPS.20151222.20151222.140101.txt
RATE_MEDD_SETUPS.20151222.20151222.150102.txt   RATE_MEDD_SETUPS.20151222.20151222.081212.txt
RATE_MEDD_SETUPS.20151222.20151222.121212.txt

I want that code return two latest file
Code:
RATE_STATE_SETUPS.20151222.20151222.170101.txt
RATE_MEDD_SETUPS.20151222.20151222.150102.txt

Code:
ls | awk -F "." '{ OFS=".";print $3,$4 }' | sort -n -k 2,2 | tail -1

My code return only one file
Code:
RATE_STATE_SETUPS.20151222.20151222.170101.txt

My OS is AIX 6
Thanks in advance
Moderator's Comments:
Mod Comment As requested several times before, please use CODE tags for sample input, sample output, and code; not just for code.

Last edited by Don Cragun; 01-28-2016 at 02:26 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 01-28-2016
tail -1 will only display the last one. tail -2 will display last two.
# 3  
Old 01-28-2016
Thanks in advance
tail -2 return two file but I have to handle the film type there is two type in example STATE AND MEdd I want that code return one from state type and another from medd type on the basis of latest timestamp
# 4  
Old 01-28-2016
You could try:

EDIT: I did misunderstand, fixing now
Fixed, note sure if I got it right, though.

Code:
ls|awk '/MEDD/ {MEDD=$1};/STATE/ {STATE=$1};{print MEDD,STATE}'|tail -1

Hope this helps

Last edited by sea; 01-28-2016 at 12:46 PM.. Reason: misunderstanding
# 5  
Old 01-28-2016
Use ls' option -t to sort according to the files' time stamps. Assuming the file names' second and third last part being their modification time,
Code:
ls -lat RA* | head -2
-rw-rw-r-- 1 coerdtr coerdtr 0 Dez 22 17:01 RATE_STATE_SETUPS.20151222.20151222.170101.txt
-rw-rw-r-- 1 coerdtr coerdtr 0 Dez 22 15:01 RATE_MEDD_SETUPS.20151222.20151222.150102.txt

would yield the desired.
# 6  
Old 01-28-2016
Quote:
Originally Posted by MOHANP12
Hi

I want to fetch the latest file form the list
example


example= filename RATE_STATE_SETUPS.20151222.ccyymmdd.hhmmss.txt
File pick which have latest ccyymmdd.hhmmss


list of file in directory are
Code:
RATE_STATE_SETUPS.20151222.20151222.170101.txt
RATE_STATE_SETUPS.20151222.20151222.140101.txt
RATE_MEDD_SETUPS.20151222.20151222.150102.txt   RATE_MEDD_SETUPS.20151222.20151222.081212.txt
RATE_MEDD_SETUPS.20151222.20151222.121212.txt

I want that code return two latest file
Code:
RATE_STATE_SETUPS.20151222.20151222.170101.txt
RATE_MEDD_SETUPS.20151222.20151222.150102.txt

Code:
ls | awk -F "." '{ OFS=".";print $3,$4 }' | sort -n -k 2,2 | tail -1

My code return only one file
Code:
RATE_STATE_SETUPS.20151222.20151222.170101.txt

My OS is AIX 6
Thanks in advance
Moderator's Comments:
Mod Comment As requested several times before, please use CODE tags for sample input, sample output, and code; not just for code.
Your script does not produce that output; it would only print:
Code:
20151222.170101

And that would be from performing an alphanumeric sort on the strings:
Code:
20151222.170101
20151222.140101
20151222.150102
20151222.081212
20151222.121212

not from a numeric sort on:
Code:
170101
140101
150102
081212
121212

I would guess that you wanted to include the date as well as the time in the sort anyway, so you may have accidentally gotten the date and timestamp you wanted.

If you need to sort on the date specified by the file names instead of by the files' timestamps, you might want something more like:
Code:
ls RATE_STATE* | sort -t '.' -k3,4nr | head -1
ls RATE_MEDD* | sort -t '.' -k3,4nr | head -1

If it is something you'll be doing frequently, you could replace two invocations of head, one invocation of ls, and two invocations of sort with one invocation of awk with something like:
Code:
ls RATE* | awk -F. '
{	if($3 > dt[$1] || ($3 == dt[$1] && $4 > ts[$1])) {
		dt[$1] = $3
		ts[$1] = $4
		last[$1] = $0
	}
}
END {	for(i in last)
		print last[i]
}'

which, with your sample filenames, would produce the output:
Code:
RATE_MEDD_SETUPS.20151222.20151222.150102.txt
RATE_STATE_SETUPS.20151222.20151222.170101.txt

but the order in which these two lines will be printed is unspecified.

Last edited by Don Cragun; 01-28-2016 at 03:35 PM.. Reason: Fix typo: s/alphanumeric/alphanumeric sort/
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to fetch values in csv

I need to fetch below values from this file. (1 Reply)
Discussion started by: nit42
1 Replies

2. Shell Programming and Scripting

How to Fetch latest version form a file?

Hi, I have a file where versions will be updated, i need to get latest/last updated version from that file. Could you please help? File looks like below - <versions> <version>R20180417.006</version> <version>R20180421.007</version> <version>R20180421.008</version> ... (5 Replies)
Discussion started by: schandra128
5 Replies

3. Shell Programming and Scripting

How can I fetch few parameters in Shell script?

Hi All, I want to fetch few details out of a huge output of AWS CLI tools : I am using this command : ec2-describe-instances (6 Replies)
Discussion started by: Palak Sharma
6 Replies

4. Shell Programming and Scripting

Find the latest file based on the date in the filename

Hi, We've a list of files that gets created on a weekly basis and it has got a date and time embedded to it. Below are the examples. I want to find out how to get the latest files get the date and time stamp out of it. Files are PQR123.PLL.M989898.201308012254.gpg... (1 Reply)
Discussion started by: rudoraj
1 Replies

5. Shell Programming and Scripting

Get latest filename without extension

I need to write a shell script to display the output of ls command like this ls -ltr *txt I get this -rw-r----- 1 oracle dba 51912704 Dec 11 10:27 /usr/local/sam/test12112012101247AM.txt -rw-r--r-- 1 oracle dba 7 Dec 11 11:58 /usr/local/sam/test.txt but I just need the latest... (7 Replies)
Discussion started by: sumang24
7 Replies

6. Shell Programming and Scripting

How to fetch data from oracle in unix shell script

Hi, How to fetch data from oracle database in unix shell scripting. list=`sqlplus -s ds_user/dsuser@EMI <<EOF set feedback off set serveroutput on set heading off set pagesize 0 set tab off select IP_ID from table / exit EOF` The output is not what i expected.I need output in... (4 Replies)
Discussion started by: Anusha_Reddy
4 Replies

7. UNIX for Dummies Questions & Answers

get the latest file by reading the date in the filename.

Hi, I grep for a pattern in a list of files. "grep -i -l $pattern *.datx*" it may give me n number of files. say for eg, it gives me 2 files. lock_eicu_20071228_00000000.dat_20071228_05343100 lock_eicu_20080501_00000000.dat_20080501_05343900 out of these 2 files I need to get the... (7 Replies)
Discussion started by: prsshini
7 Replies

8. Shell Programming and Scripting

shell script to find latest date and time of the files

Hi everyone, Please help:) I have a list of 1000 different files which comes daily to the directory.Some of the files are not coming to the directory now. I need to write a shell script to find the latest date and time of the files they came to the directory. The files should be unique.... (1 Reply)
Discussion started by: karthicss
1 Replies

9. Shell Programming and Scripting

fetch hostname and instance name using shell script

Hi All, Requirement is to fetch hostname and instance name using shell script from all configuration files on a server R12 on IBM AIX... could anyone please share such an experience encountered before.Is there such a script available in this forum or any other site.. Thanks for your time!... (0 Replies)
Discussion started by: a1_win
0 Replies

10. Shell Programming and Scripting

Shell script to ftp latest file

Hello All, Hope u r doing fine. I'm writing a shell script to ftp the latest file having pericular convention as 'ULTI_15072007043205.txt' on daily basis. Now the date & timing of the file geleration isnt constant, so the file name daily varies as per the date & timing of generation. Can anyone... (7 Replies)
Discussion started by: im_new
7 Replies
Login or Register to Ask a Question