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
# 8  
Old 03-19-2018
Perhaps i'm missing something in your requirement, but this script will print files in the current directory for the date string passed as argument #1 (defaults to today's date if no parameters passed):

Code:
DT=${1:-$(date +%Y%m%d)}
for file in *_${DT}.csv
do
   [ -f "$file" ] && echo "$file"
done

This User Gave Thanks to Chubler_XL For This Post:
# 9  
Old 03-20-2018
Hi

I had tried this, but I think the code is reading the string after the first occurrence of "_" as the basis to identify the date. However if few of my files has the naming convention of following, how do we deal with it?

Code:
abcdefgh_20180102.csv
abcdefgh_20180120.csv
xyz_20180121.csv
xyz_20180102.csv
abcdefgh_xyz_pqr_20180102.csv
abcdefgh_xyz_pqr_20180109.csv
abcdefgh_65325_parent_20180120.csv
abcdefgh_65325_parent_20180127.csv
xyz_34567_filter_20180121.csv
xyz_34567_filter_20180128.csv

Thanks & regards,
SK

Last edited by vgersh99; 03-20-2018 at 11:19 AM.. Reason: code tags, please!
# 10  
Old 03-20-2018
Untested
Code:
ls *.csv  |
{ while read LINE    
    do  GR=${LINE%_*}
        DT=${LINE#${GR}_}
        DT=${DT%.*}
        XT=.${LINE##*.}
        [ ! "$GR" = "${OGR:-$GR}" ] &&          { echo ${OLN} 
                                                  ODT=""   
                                                }          
        [ "$DT" -gt "$1" ] && [ "$ODT" ]   ||     ODT=$DT
        OGR=$GR
        OLN="$LINE"  
  done  
echo ${OLN} 
}

This User Gave Thanks to RudiC For This Post:
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