2015-02-09 110 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