2015-02-09 76 views
0

我有这个脚本将读取将在目录中的csv文件。不过我希望脚本它移动到第二列即用于验证列的shell脚本

Name, City 
Joe, Orlando 
Sam, Copper Town 
Mike, Atlanta 

所以它必须首先检查coulmn将其命名为moveds在检查前市之前验证.csv文件的整个coulmn?我将如何更改以下脚本来迎合此?

 # Read all files. no file have spaces in their names 
    for file in /source/*.csv ; do 
     # init two variables before processing a new file 
    FILESTATUS=GOOD 
    FIRSTROW=true 
     # process file 1 line a time, splitting the line by the 
     # Internal Field Sep , 
    cat "${file}" | while IFS=, read field1 field2; do 
     # Skip first line, the header row 
    if [ "${FIRSTROW}" = "true" ]; then 
    FIRSTROW=FALSE 
     # skip processing of this line, continue with next record 
    continue; 
    fi 

     #different validations 
    if [[ "${field1}" = somestringprefix* ]]; then 
    ${FILESTATUS}=BAD 
    # Stop inner loop 
    break 
    fi 
    somecheckonField2 
    done 
    if [ ${FILESTATUS} = "GOOD" ] ; then 
     mv ${file} /source/good 
    else 
     mv ${file} /source/bad 
    fi 
done 

回答

0

我会在内部循环使用awk

if awk -F, '$1 !~/^prefix1/ || $2 !~ /^prefix2/ {exit(1)}' "$file" ; then 
    mv "$file" good 
else 
    mv "$file" bad 
fi 

^prefix1^prefix2意味着是正则表达式模式。

+0

感谢但上面的脚本,它迭代垂直检查列中的每个字段,而不是水平这是传统的评价 – 2015-02-09 16:45:07

+0

它逐行验证。但是如果一行无效,它会停止对该文件的进一步处理并返回'1' – hek2mgl 2015-02-09 16:48:08