Shell program question with Filenames and dates


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell program question with Filenames and dates
# 1  
Old 03-19-2018
Shell program question with Filenames and dates

Hi Unix Gurus

Let's say I have the input files like the following. I need to pick the files based on my run date.

Code:
abcd_20180206.csv
abcd_20180213.csv
abcd_20180220.csv
abcd_20180227.csv
efgh_20180206.csv
efgh_20180220.csv
efgh_20180227.csv
ijkl_20180206.csv
ijkl_20180213.csv
ijkl_20180220.csv
ijkl_20180227.csv
mnop_20180206.csv
mnop_20180213.csv
mnop_20180220.csv
mnop_20180221.csv
mnop_20180227.csv
qrst_20180206.csv
qrst_20180213.csv
qrst_20180220.csv
qrst_20180227.csv
uvwx_20180206.csv
uvwx_20180213.csv
uvwx_20180220.csv
uvwx_20180227.csv
yyzz_20170606.csv

So if I am running for 20180206 or 20180209 or 20180212, The output of the above should be:

Code:
abcd_20180206.csv
efgh_20180206.csv
ijkl_20180206.csv
mnop_20180206.csv
qrst_20180206.csv
uvwx_20180206.csv
yyzz_20170606.csv

And if I am running for 20180213 or 20180215 or 2018019, The output of the above should be:

Code:
abcd_20180213.csv
efgh_20180206.csv
ijkl_20180213.csv
mnop_20180213.csv
qrst_20180213.csv
uvwx_20180213.csv
yyzz_20170606.csv

Also if I am running for 20180221, The output of the above should be:

Code:
abcd_20180220.csv
efgh_20180221.csv
ijkl_20180220.csv
mnop_20180220.csv
qrst_20180220.csv
uvwx_20180220.csv
yyzz_20170606.csv

Can someone please help me with this?

Thanks & regards,
SK


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

Last edited by RudiC; 03-19-2018 at 02:21 PM.. Reason: Added CODE tags.
# 2  
Old 03-19-2018
This is far from clear. Does it mean "for each of the groups defined by the first four characters, select the file with date stamp less or equal to the date parameter string given"?
# 3  
Old 03-19-2018
The first few characters of the file name can vary. Few file names can have 8 letters and others may have 80 letters.

However it is the last 8 characters representing the date (before .csv) and to take the latest available dated file based on the run date.
# 4  
Old 03-19-2018
You don't seem to bother much explaining your problem. Why should anyone in here care more about your problem than you do?
# 5  
Old 03-19-2018
May be I am not phrasing my words correctly here. I will have bunch of files with various versions of each file. For example, my list may contain:
Code:
abcdefgh_20180102.csv
abcdefgh_20180120.csv
xyz_20180121.csv
xyz_20180102.csv

So if I am passing a variable of (20180118) should give me:
Code:
abcdefgh_20180102.csv
xyz_20180102.csv

and the variable (20180121) should give me:
Code:
abcdefgh_20180120.csv
xyz_20180121.csv

The timestamps of the files has to be ignored as the filenames can be past dated with current timestamps. So I should only look at the filenames and the last 8 characters of the filename representing the date.

Sorry if I am not able to explain clearly.

Regards,
SK

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

Last edited by RudiC; 03-19-2018 at 03:28 PM.. Reason: Added CODE tags.
# 6  
Old 03-19-2018
With the "run date" as the first positional parameter, and assuming you are using a not too old bourne compatible shell, try
Code:
ls *.csv | {
while IFS="_." read GR DT XT
  do    [ ! "$GR" = "${OGR:-$GR}" ] &&          { echo ${OGR}_${ODT}.${OXT}
                                                  ODT=""
                                                }
        [ "$DT" -gt "$1" ] && [ "$ODT" ]   ||     ODT=$DT
        OGR=$GR
        OXT=$XT
  done
echo ${OGR}_${ODT}.${OXT}
}

This User Gave Thanks to RudiC For This Post:
# 7  
Old 03-19-2018
Thanks a lot for your reply. I will try this.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help on Dates in Shell Scripting

Hi Experts, I am using the below code to get the previous day based on the date given as a input. #!/usr/bin/ksh datestamp=`date '+%Y%m%d'` yest=$((datestamp -1)) echo $yest Output: 20130714 How can i display the same output in 14/07/2013, i tired '+%d/%m/%y'` but i am getting... (16 Replies)
Discussion started by: learner24
16 Replies

2. Shell Programming and Scripting

Storing filenames in an array in shell script

hi, i am writing a shell script in which i read a line in a variable. FNAME="s1.txt s2.txt s3.txt s4.txt s5.txt" i want to create a array and store single file names in a array.. so the array should contain arr="s1.txt" arr="s2.txt" arr="s3.txt" arr="s4.txt" arr="s5.txt" how to... (3 Replies)
Discussion started by: Little
3 Replies

3. Shell Programming and Scripting

Awk program for calculating dates.

Hi All, I have a txt file which has hundreds of lines and 41 fields. I have a requirement to pick up field 14 from the text file which is a date fiels in the following format. Field 14 : Data Type : NUMERIC DATE (YYYYMMDD) Field Length : 8 Example of Data :20090415 Field 42 : Data Type... (2 Replies)
Discussion started by: nua7
2 Replies

4. Shell Programming and Scripting

Filenames in CSH shell

Hi, I am running into a bit of a problem. I am trying to write a script that will run an interactive (fortran) program repeatedly to dump data across 36 data channels... anyway. I have gotten this far... #!/bin/csh set i=0 while ($i<2) fitsfilter $1<<EOF $i 0 9 1 1 g 2 1250 -100... (5 Replies)
Discussion started by: madtowneast
5 Replies

5. Shell Programming and Scripting

Spaces in filenames located in variables in shell.

Greetings. I am trying to do a script that will do some file copying for me. Unfortunately I have spaces in the directory names (which I cannot change) and the result is someone hard to achieve in shell scripts. I have searched everywhere on the web but does not manage to find the answer to... (3 Replies)
Discussion started by: Mr.Glaurung
3 Replies

6. UNIX for Dummies Questions & Answers

Question/review my script: removing bad chars from filenames

The task: remove undesirable characters from filenames. Restrictions: Must use basic RE, base utilities (non-GNU) and /bin/sh (ash). No ksh, zsh, perl, etc. Below is what I've come up with. It seems to work OK but I'm open to shorter, more efficient alternatives. Inside the square... (4 Replies)
Discussion started by: uiop44
4 Replies

7. Shell Programming and Scripting

Question on reading dates in folders/files

Hi All, I have some folder that are named "FOLDERYYYYMM". I'm trying to piece together a .sh script that will look for the folder with the date. How can I get shell to see the date? (3 Replies)
Discussion started by: bbbngowc
3 Replies

8. Shell Programming and Scripting

Awk question: Sum dates

Hi there, I have a logfile which has the following layout: 20080812 0 20 20080812 12 10 20080812 12 10 20080812 12 10 I want to sum the "12" on the last 3 lines and save the "20" on the first line. The final output should be 20080812 36 20 I think that should me more easier with... (6 Replies)
Discussion started by: BufferExploder
6 Replies

9. UNIX for Dummies Questions & Answers

While we are on the subject of dates. Another date question

This goes deeper into the date thing. I want to be able to check the date and time stamp in or on a file to see what the time span is. We have a job that runs several times an hour - kicked off through cron based on a trigger file. We want to keep track of each run and check the time between... (14 Replies)
Discussion started by: MizzGail
14 Replies

10. UNIX for Dummies Questions & Answers

reading filenames inside a program

UNIX Sun Ultra60 5.5.1 Hello everybody, I have a problem that seems simple but turns out to be complex (for me at least). My program needs to open a directory (this part is easy), scan each filename and determine whether or not a file with the suffix (.07) exists. So the program would return... (5 Replies)
Discussion started by: j_t_kim
5 Replies
Login or Register to Ask a Question