Script logic


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script logic
# 1  
Old 01-03-2011
Script logic

Please help me to write the following script.
1) Input files will be in Test directory. (Test/YYYY/MM) folders (Ex: 1) 2010/ 01 2) 2011/05)
The file name will be Testfile1_6676543218_Axxxxxxyyyyyzzzzzzzzzzzzzzzzzz.pdf.gz
2) The compare file will be in /tmp directory. (/tmp/comparefile.txt). The file will have list of 6 digit numbers. Ex:
(001234
015678
045679)
3) The batch has to compare 6 digit numbers in the compare file and 6 digit numbers in PDF file (xxxxxx - position 23 - 28) in the Test directory folders(Current month - Previous months). If it matches, then copy the pdf file to the new folder.

1) Conditions: It has to look for (Current month - Previous months) folder. Previous month should read from the property file. It can be either (01 ....12 months)
Ex: if Current month - 01 2011 and Previous month - 06 2010 then batch has to look since Aug 2010 to Jan 2011 ) folder

Thanks
# 2  
Old 01-03-2011
(Is this schoolwork?)

Can you please provide actual samples of the test directory listing and comparefile.txt.
# 3  
Old 01-03-2011
The compare file will have around 200 store numbers.
Ex: (001345
013467
027890
011456)

The pdf files will be under Test/2010/01 -->
Testfile1_6676543218_A0013457899000232323232323232323232.pdf.gz
Testfile1_6676543218_A0013447899000232323232323232323232.pdf.gz
Testfile1_6676543218_A0134677899000232323232323232323232.pdf.gz

Test/2010/02(month) -->
Testfile1_6676543218_A0012117899000232323232323232323232.pdf.gz
Testfile1_6676543218_A0034597899000232323232323232323232.pdf.gz
Testfile1_6676543218_A0013457899000232323232323232323232.pdf.gz

if current month is 01 and previous month is 06 ..then batch has to look for all the folders since Aug(2010/05 folder to 2011/01 folder).
Hope it clears. if 001345 matches(position 23 - 28) any of the above folder , then it should copy to the new folder(sample folder)

hope you understand.
# 4  
Old 01-03-2011
How many folders?
How many files in each folder?
# 5  
Old 01-03-2011
There will be 12 folders under every year(month 01 - 12)

Lot of pdf files will be in each folder(i think Average 5000 files ) not sure the exact count..
# 6  
Old 01-03-2011
@mjnx
Thak you for stating the scale. With a potential 60,000 files per year, this is clearly not a homework question.
# 7  
Old 01-03-2011
My proposed solution:
Code:
#! /bin/ksh

NAME=$(basename ${0})
FILE=$(mktemp --tmpdir=/tmp ${NAME}XXXXXX)

trap "rm -f ${FILE}.\*" EXIT

#-------------------------------------------------------------------------------

comparefile=
monthprior=
movetodir=
verbose=

while getopts :d:f:p:v OPT; do
    case "${OPT}" in

        d)  if [[ ! -d "${OPTARG}" ]]; then
                print -u2 ${NAME}: not a directory: "${OPTARG}"
                exit 1
            fi

            if [[ ! -w "${OPTARG}" || ! -x "${OPTARG}" ]]; then
                print -u2 ${NAME}: permission denied: "${OPTARG}"
                exit 1
            fi

            movetodir="${OPTARG}"
            ;;

        f)  if [[ "${OPTARG}" != - && ! -e "${OPTARG}" ]]; then
                print -u2 -- ${NAME}: no such file or directory - "${OPTARG}"
                exit 1
            fi

            comparefile="${OPTARG}"
            ;;

        p)  if [[ "${OPTARG}" != [1-9][0-9]* ]]; then
                print -u2 ${NAME}: invalid number of months: "${OPTARG}"
                exit 1
            fi

            monthsprior=${OPTARG}
            ;;


        v)  verbose=verbose ;;

        *)  print -u2 -- "USAGE ${NAME} [-p <monthsprior>] [-c <comparefile] [-v] dir..."
            exit 1
            ;;
    esac
done

shift $(expr ${OPTIND} - 1)

if [[ -z "${movetodir}" ]]; then
    print -u2 -- ${NAME}: missing destination directory
    exit 1
fi

#-------------------------------------------------------------------------------
# build list of YYYY/MMs to process

current=$(date +%Y/%m)

typeset -Z4 Y=${current%/*}
typeset -Z2 M=${current#*/}

while [[ 0 -lt ${monthsprior} ]]; do
    (( monthsprior = monthsprior - 1 ))

    (( M = M - 1 ))

    if [[ ${M} -lt 1 ]]; then
        M=12
        (( Y = Y - 1 ))
    fi

    print -- ${Y}/${M}
done > ${FILE}.ym

#-------------------------------------------------------------------------------
# validate directories

for dir in "${@}"; do

    if [[ ! -d "${dir}" ]]; then
        print -u2 -- "${dir}:" no such directory
        continue
    fi

    if [[ ! -r "${dir}" || ! -w "${dir}" || ! -x "${dir}" ]]; then
        print -u2 -- "${dir}:" permission denied
        continue
    fi

    print -- "${dir}"
done > ${FILE}.dirs

#-------------------------------------------------------------------------------

grep -v '^#' "${comparefile:--}" | while read key; do

    if [[ -n "${verbose}" ]]; then
        print -- '*' "${key}"
    fi

    for dir in $(< ${FILE}.dirs); do
        if [[ -n "${verbose}" ]]; then
            print -- '/' "${dir}"
        fi

        for ym in $(< ${FILE}.ym); do
            if [[ ! -d "${dir}/${ym}" ]]; then
                continue
            fi

            ls "${dir}/${ym}" \
            | grep "_A${key}[0-9]*.pdf.gz" \
            | while read ${file}; do

                if [[ -n "${verbose}" ]]; then
                    print -- '+' "${ym}/${file}"
                fi

                mv -f "${dir}/${ym}/${file}" "${movetodir}"
            done
        done
    done
done

Usage:
Code:
scriptname -d destinationdirectory -p numberofmonths [ -c comparefile ] dirs....

If comparefile is not provided or is set to "-", then the list will be read from standard input. Lines beginning with "#" are ignored.

Caveat emptor

While I have done my best to test this, please make sure you understand what is going on before you run this in production. At a minimum, change:
Code:
mv -f "${dir}/${ym}/${file}" "${movetodir}"

to
Code:
print -- mv -f "${dir}/${ym}/${file}" "${movetodir}"

to see what would be done.

Notes

Let me know if you have any questions.

I would appreciate knowing if there are any kshisms I did not take advantage of.
Login or Register to Ask a Question

Previous Thread | Next Thread

2 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need script logic

i have two csv files (rates.csv, reference.csv) as below. rates.csv contains Code, respective Zone details rates.csv ---------- 23,38Nhava 45,37NEWYORK 89,SHILANG 71,ROBACCA reference.csv contains all Zone details reference.csv ------------- 37Newyork robacca 38Nhava... (5 Replies)
Discussion started by: p_satyambabu
5 Replies

2. Solaris

Need script logic

Hi, i have a file which contains names of persons and date as below. peter 27-mar Vicky 08-sep Saryu 03-jan venki 14-dec ..... ... can someone plz provide a script logic, which returns the Name of the person whose date is matching with current date. (5 Replies)
Discussion started by: JSKOBS
5 Replies
Login or Register to Ask a Question