简体   繁体   中英

reading in a file in a loop and shell scripting

if [ ${FILESTATUS} = "GOOD" ] ; then
    mv ${file} /export/home/goodFile
else
    mv ${file} /export/home/badFile
fi

want the above to be integrated to the below script. If both column pass the validation then THAT FILE(.csv) should be moved to the good file directory otherwise it should be moved bad file. Please help with integrating the logic/loop

for file in /export/home/*.csv ; do
awk -F', ' '
    # skip the header and blank lines
    NR == 1 || NF == 0 {next}

    # save the data
    { for (i=1; i<=NF; i++) data[++nr,i] = $i }

    END {
        status = "OK"

        # verify column 1
        for (lineno=1; lineno <= nr; lineno++) {
            if (length(data[lineno,1]) == 0) {
                status = "BAD" 
                break
            }
        }
        printf "file: %s, verify column 1, status: %s\n", FILENAME, status

        #verify coulmn 2
        for(linenum = 1; linenum <nr; linenum++) {
        if (length(dataArr[linenum,2]) == 0){
        STATUS = "BAD"
        break
        }

        if ((dataArr[linenum,2]) !~ /^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)[0-9][0-9]$/){
        STATUS = "BAD"
        break
        }
    }

        # verify other columns ...
    }
' "$file"
done

Have this script that is supposed to read in about 10 or so .csv files from a directory. However I want this script to integrate the following where If the file is succesfully passed through validation steps it goes to the goodFile directory other wise goes to the badfile directory. I am not sure where to include this looping mechnaism.

Anywhere in the awk code you write STATUS = "BAD" , replace that with exit 1

Then, in the for loop, test awk's exit status

for file in /export/home/*.csv ; do
    awk -F', ' '....' "$file"

    if [[ $? -eq 0 ]]; then
        # "good" status
        mv ${file} /export/home/goodFile
    else
        # "bad" status
        mv ${file} /export/home/badFile
    fi
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM