Should pick latest file within past 3 days using UNIX script and perform steps in message below.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Should pick latest file within past 3 days using UNIX script and perform steps in message below.
# 8  
Old 12-05-2017
Quote:
Originally Posted by Corona688
Step 1 is a problem since you can't have more than one file with the same name.
True, but if you look closely, you can see that the first file has a leading space in its name. Smilie
# 9  
Old 12-05-2017
Hi Rudic Thank you for explanation.I understood your logic except the below.

As mentioned We will have multiple files in a folder with different file names we should pick only the files which start with abc_*_*.dat which came with in last 3 days.Can you please tell how we are doing this?

And also filetimestamp and the $THREEDAYSAGO values/format is fifferent .how we are comparing both? in if condition?Can you explain with example.

Filenames in folder:

Code:
1)abc_20171204.052389.dat
2)abc_20171204.052428.dat
3)def_20171204.052440.dat
4)ghf_20171204.054340.dat

Code:
C:

for file in *_*_*.dat
do
   FILESTAMP=${file#*_}
   FILESTAMP=${FILESTAMP%_*}

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

And, DON'T modify posts (here: #1) if others already referenced it, pulling the rug from under their feet!

Last edited by sunnykamal59; 12-05-2017 at 01:55 PM.. Reason: Added CODE tags and descritpion
# 10  
Old 12-05-2017
My first and most important advice is that you apply utmost care when creating a post - watch your spelling, upper / lower case, punctuation, and data accuracy. Why should anybody care more for your posts than you do? Why should anybody want to guess what your problems are?
In your post#9:
- what is the C: for?
- you write "files which start with abc_*_*.dat" but post sample files like ghf_20171204.054340.dat - neither the abc nor the second _ appears; so: which one is valid? The code doesn't work with a wrong pattern.
- with "filetimestamp" you mean FILESTAMP? The latter is not directly compared to $THREEDAYSAGO but converted to seconds before the compare.
# 11  
Old 12-05-2017
I agree.Below are the file names we receive in folder.

Filename in a folder:


Code:
abc_20171204.052389.dat
abc_20171204.052428.dat
def_20171204.052440.dat
ghf_20171204.054340.dat


Can you please explain how we are getting the 'FILESTAMP' from above filenames and converting into seconds?


and what will be the result for below code?

Code:
for file in *_*_*.dat
do
   FILESTAMP=${file#*_}
   FILESTAMP=${FILESTAMP%_*}


Last edited by Scott; 12-05-2017 at 02:58 PM.. Reason: Code tags (properly) added
# 12  
Old 12-05-2017
The result for the cited code is unpredictable as it depends on the number of files in that directory containing at least one _ character (disregarding the .dat ending). As said in post#10, the pattern does NOT match your target files.

Look into your shell's (which version you fail to mention, btw) man page for "Parameter expansion; Remove matching prefix / suffix pattern." to see how ${file#*_} and ${FILESTAMP%_*} are used to extract the time stamp from the file names. Also, use echo in the loop to print out the intermediate states of the extraction.
In post#6 you see date -d $FILESTAMP +'%s' used to convert the time stamp into epoch seconds for the comparison.
# 13  
Old 12-06-2017
Hopefully this will be my last question .

1)If we have multiple set of files in a folder like below, how we are looping each file for doing comparison?

Code:
abc_20171204.052389.dat
abc_20171203.052428.dat
abc_20171202.052628.dat


2)for looping are we doing below logic in script?

Code:
for file in *_*_*.dat
do
   FILESTAMP=${file#*_}
   FILESTAMP=${FILESTAMP%_*}


3)if so can you please explain the above code with an example?
# 14  
Old 12-06-2017
Quote:
Originally Posted by sunnykamal59
Hopefully this will be my last question .

1)If we have multiple set of files in a folder like below, how we are looping each file for doing comparison?

Code:
abc_20171204.052389.dat
abc_20171203.052428.dat
abc_20171202.052628.dat


2)for looping are we doing below logic in script?

Code:
for file in *_*_*.dat
do
   FILESTAMP=${file#*_}
   FILESTAMP=${FILESTAMP%_*}


3)if so can you please explain the above code with an example?
In all of your early posts you said that files were named using the pattern abc_YYYYMMDD_hhmmss.dat and the code that RudiC suggested processes the files that match the pattern you supplied.

When you then try running the code he suggested in a directory that doesn't have any files that match that pattern, the code won't find any files that match the pattern you told us all that you wanted to process. This is a classic example of what happens when you supply sample data that doesn't match your real data.

Feel free to modify the code RudiC suggested to match the format of the real filenames you are trying to process. Do not blame RudiC for supplying code that looked for what you said should be looked for when no files in that format are present.

To make the code match the latest sample names you have provided, you'll need to change the filename matching pattern in the for statement and change the parameter expansion that is used to extract the date from the expansion of the shell FILESTAMP variable.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I have this list of files . Now I will have to pick the latest file based on some condition

3679 Jul 21 23:59 belk_rpo_error_**po9324892**_07212014.log 0 Jul 22 23:59 belk_rpo_error_**po9324892**_07222014.log 3679 Jul 23 23:59 belk_rpo_error_**po9324892**_07232014.log 22 Jul 22 06:30 belk_rpo_error_**po9324267**_07012014.log 0 Jul 20 05:50... (5 Replies)
Discussion started by: LoneRanger
5 Replies

2. Shell Programming and Scripting

to pick the latest file modified in a directory

I wan to pick the latest modified file name and redirect it to a file .. ls -tr | tail -1 >file but this is printing file ins side the filename , can anyone help me out (5 Replies)
Discussion started by: vishwakar
5 Replies

3. UNIX for Dummies Questions & Answers

Need command to pick the latest file

Hi In my script i am trying to access mainframe server using FTP, in the server i have filee with the timestamp.I need to get the file with the latest timestamp among them . The server has the below files / ftp> cd /outbox 250 CWD command successful ftp> ls 200 PORT command successful... (4 Replies)
Discussion started by: laxmi131
4 Replies

4. UNIX for Dummies Questions & Answers

pick the very latest directory

Hi, I have some list of directories in the form datemonthyear e.g. 02082009, 03082009 and 04082009 etc. I need to pick the latest directory from the current working directory. Outcome: 05082009 This is the output am expecting. Thanks (6 Replies)
Discussion started by: venkatesht
6 Replies

5. Shell Programming and Scripting

how can i pick the latest log file as per below

in the below .. i want to pick the latest logfile which is having JPS.PR inside.. that means i want particularly "spgport040408041223.log:@@@@@@@@ 04:13:09 Adding: JPS.PR." which is latest among these.. is it possible to compare the current time with logfile time ? reptm@xblr0758rop>... (4 Replies)
Discussion started by: mail2sant
4 Replies

6. UNIX for Dummies Questions & Answers

unix script that will pick up first 10 file

Suppose I have a unix file which contain a lost of 60 files like filename1 filename2 ... .. ... filename60 I want to write a unix script that will pick up first 10 files in first run 10-20 files in 2 run 20-30 files in 3 run 30-40 files in 4 run 40-50 files in 5 run 50-60 files in 6... (1 Reply)
Discussion started by: er_zeeshan05
1 Replies

7. Shell Programming and Scripting

find files from the past 7 days

Hi All, I have a file which contains the listing of another directory: >cat list.dat -rwxr-xr-x 1 test staff 10240 Oct 02 06:53 test.txtdd -rwxrwxrwx 1 test staff 0 Oct 04 07:22 test.txx -rwxrwxrwx 1 test staff 132 Sep 16 2007 test_tt.sh... (6 Replies)
Discussion started by: deepakgang
6 Replies

8. UNIX for Dummies Questions & Answers

creating a CSV file for past 7 days

I have a requirement which will select the files with a specific naming convention which got created in past 7 days in a specific directory.Lets say the directory is /data/XYZ and the file names follow the below nomenclature like Daily_File*.txt I just need to create one CSV file which will... (12 Replies)
Discussion started by: dr46014
12 Replies

9. Shell Programming and Scripting

Script to pick out files from a file which are older than 15 days

Hi , I have a file abc.txt which actually contains a directory and file list -rwxrwxr-x 1 dmadmin mmasapp 334 Dec 03 2001 aafs_006.ksh -rwxrwxr-x 1 dmadmin mmasapp 1270 Nov 13 2001 mcl_deposit_ftp.ksh -rwxrwxr-x 1 dmadmin mmasapp 925 Oct 31 2001 ... (1 Reply)
Discussion started by: viv1
1 Replies

10. Shell Programming and Scripting

ls latest 4 days or specify days of files in the directory

Hi, I would like to list latest 2 days, 3 days or 4 days,etc of files in the directory... how? is it using ls? (3 Replies)
Discussion started by: happyv
3 Replies
Login or Register to Ask a Question