简体   繁体   English

在循环和shell脚本中读取文件

[英]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. 如果两列都通过了验证,则应将THAT FILE(.csv)移动到正确的文件目录,否则应将其移动到坏文件中。 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. 这个脚本应该从目录中读取大约10个左右的.csv文件。 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. 但是,我希望此脚本集成以下内容如果文件成功通过验证步骤,则转到goodFile目录,否则将转到badfile目录。 I am not sure where to include this looping mechnaism. 我不知道在哪里包括这种循环机制。

Anywhere in the awk code you write STATUS = "BAD" , replace that with exit 1 你写的awk代码中的任何地方STATUS = "BAD" ,用exit 1替换它

Then, in the for loop, test awk's exit status 然后,在for循环中,测试awk的退出状态

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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