Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

How to find list of missing files based on the file format?

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #8  
Old Unix and Linux 05-19-2017
nalu nalu is offline
Registered User
 
Join Date: Apr 2017
Last Activity: 5 June 2017, 7:28 AM EDT
Posts: 25
Thanks: 1
Thanked 0 Times in 0 Posts
Hi Don,

Pleasde find the details as below.

What operating system (including release number) are you using?

Code:
Linux dev.voda.mp.com 2.6.18-400.1.1.el5 #1 SMP Sun Dec 14 06:01:17 EST 2014 x86_64 x86_64 x86_64 GNU/Linux

What shell (including version number) are you using?

Code:
ksh

Do you have a ksh (version 93u+ or later) that I can use instead of whatever shell you're using to provide an example?

Code:
version         sh (AT&T Research) 93u+ 2010-06-21

Please help me to convert to the user file format to date format string as below.


Code:
adv_voda_%Y-%m-%d-??????_????-??-??-??????_????-??-??-??????.txt

Thanks in advance

Last edited by nalu; 05-19-2017 at 03:37 AM..
Sponsored Links
    #9  
Old Unix and Linux 05-19-2017
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 23 July 2017, 6:19 PM EDT
Location: San Jose, CA, USA
Posts: 10,412
Thanks: 527
Thanked 3,638 Times in 3,104 Posts
The following script is a quick and dirty demonstration producing results similar to what your script seemed to be trying to do. It takes the number of days to examine and the file pattern as command line arguments and prints the date in %Y-%m-%d format for dates that had no file matching the given pattern. It only uses ksh built-ins (without invoking awk, date, expr, head, sed, or wc). The missing dates are printed to standard output from this script since the redirection to missingfiles.txt is commented out.

Code:
#!/bin/ksh
IAm=${0##*/}

check_mode=1
count=0

if [[ $# -ne 2 ]]
then	printf 'Usage: %s interval filename_format\n' "$IAm" >&2
	exit 1
fi

interval=$1
file_format=$2

date_pattern=${file_format//YYYY/%Y}
date_pattern=${date_pattern//YY/%y}
if [[ "$date_pattern" == "$file_format" ]]
then	printf '%s: Invalid date format: No "YYYY" or "YY" found:\n\t"%s"\n' \
	    "$IAm" "$file_format" >&2
	exit 2
fi
date_pattern=${date_pattern//MM/%m}
date_pattern=${date_pattern//DD/%d}
date_pattern=${date_pattern//HH/%H}
date_pattern=${date_pattern//II/%M}
date_pattern=${date_pattern//SS/%S}

printf '%s: Processing interval:%d & date_pattern:\n\t"%s"\nfrom file_format:\n\t"%s"\n' \
    "$IAm" "$interval" "$date_pattern" "$file_format"

if [[ $check_mode -eq 1 ]]
then	while [[ $interval -ne 0 ]]
	do	file_pattern=$(printf "%($date_pattern)T\n" "$interval day ago")
		for path in $file_pattern
		do	if [[ -f $path ]]
			then	count=1
				break
			fi
		done
		if [[ $count -eq 0 ]]
		then	printf '%(%Y-%m-%d)T\n' "$interval day ago"
		else	count=0
		fi
		interval=$((interval - 1))
	done # >missingfiles.txt
fi

In a directory containing the files:

Code:
total 32
-rw-r--r--  1 dwc  staff     0 May 18 10:36 2017-05-10.txt
-rw-r--r--  1 dwc  staff     0 May 18 10:36 2017-05-11
-rw-r--r--  1 dwc  staff     0 May 18 10:36 2017-05-12
-rw-r--r--  1 dwc  staff     0 May 18 10:36 adb_voda_2017-05-10.txt
-rw-r--r--  1 dwc  staff     0 May 18 10:36 adb_voda_2017-05-11-150325_2017-05-10-112227_2017-05-13-112227.txt
-rw-r--r--  1 dwc  staff     0 May 18 10:36 adb_voda_2017-05-12-150325_2017-05-12-112227_2017-05-11-112227
-rw-r--r--  1 dwc  staff     0 May 18 10:36 adb_voda_2017-05-14-150325_2017-05-11-112227_2017-05-10-112227.txt
-rw-r--r--  1 dwc  staff     0 May 18 10:36 adb_voda_2017-05-16_2017-04-30_2017-05-01.txt
-rw-r--r--  1 dwc  staff     0 May 18 10:36 adb_voda_20170510.txt
-rw-r--r--  1 dwc  staff     0 May 18 10:36 asr_spir_2017-05-10-150325_2017-05-10-112227_2017-05-13-112227.txt
-rwxr-xr-x  1 dwc  staff   704 May 19 04:04 driver
-rw-r--r--  1 dwc  staff  6471 May 18 10:40 problem
-rwxr-xr-x  1 dwc  staff  1108 May 19 03:46 tester

where the above script is named tester, the command:

Code:
./tester 10 "adb_voda_YYYY-MM-DD-??????_????-??-??-??????_????-??-??-??????.txt"

when run on May 19, 2017 produces the output:

Code:
tester: Processing interval:10 & date_pattern:
	"adb_voda_%Y-%m-%d-??????_????-??-??-??????_????-??-??-??????.txt"
from file_format:
	"adb_voda_YYYY-MM-DD-??????_????-??-??-??????_????-??-??-??????.txt"
2017-05-09
2017-05-10
2017-05-12
2017-05-13
2017-05-15
2017-05-16
2017-05-17
2017-05-18

and the command:

Code:
./tester 15 "YYYY-MM-DD"

produces the output:

Code:
tester: Processing interval:15 & date_pattern:
	"%Y-%m-%d"
from file_format:
	"YYYY-MM-DD"
2017-05-04
2017-05-05
2017-05-06
2017-05-07
2017-05-08
2017-05-09
2017-05-10
2017-05-13
2017-05-14
2017-05-15
2017-05-16
2017-05-17
2017-05-18

and the command:

Code:
./tester 10 "adb_voda_????-??-??-??????_????-??-??-??????_YYYY-MM-DD-??????.txt"

produces the output:

Code:
tester: Processing interval:10 & date_pattern:
	"adb_voda_????-??-??-??????_????-??-??-??????_%Y-%m-%d-??????.txt"
from file_format:
	"adb_voda_????-??-??-??????_????-??-??-??????_YYYY-MM-DD-??????.txt"
2017-05-09
2017-05-11
2017-05-12
2017-05-14
2017-05-15
2017-05-16
2017-05-17
2017-05-18

Hopefully, you can modify this to get something that works for you.
Sponsored Links
    #10  
Old Unix and Linux 05-24-2017
nalu nalu is offline
Registered User
 
Join Date: Apr 2017
Last Activity: 5 June 2017, 7:28 AM EDT
Posts: 25
Thanks: 1
Thanked 0 Times in 0 Posts
Hi Don,

Thanks a lot.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Find list of files missing read & execute permission Catullus Shell Programming and Scripting 2 12-09-2015 01:52 PM
How to list the files based on the modification time using the find command? Kesavan Shell Programming and Scripting 6 03-06-2012 10:59 AM
Find files older than X with a weird file format MickAAA Shell Programming and Scripting 2 12-16-2010 10:07 PM
Find missing files from a list ultimatix Shell Programming and Scripting 5 08-10-2010 06:05 AM
Help with find command and list in a long format each found file alexcol Shell Programming and Scripting 5 12-10-2007 07:59 AM



All times are GMT -4. The time now is 08:52 PM.