用于验证列的 shell 脚本
A shell script to validate a column
我有这个脚本,它将读取目录中的 csv 文件。但是我希望脚本在移动到第二列之前验证 .csv 文件的整个库,即
Name, City
Joe, Orlando
Sam, Copper Town
Mike, Atlanta
所以它必须先检查列名,然后再继续检查城市?我将如何更改以下脚本来满足此要求?
# 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
我会在内循环中使用 awk
:
if awk -F, ' !~/^prefix1/ || !~ /^prefix2/ {exit(1)}' "$file" ; then
mv "$file" good
else
mv "$file" bad
fi
^prefix1
和 ^prefix2
是正则表达式模式。
我有这个脚本,它将读取目录中的 csv 文件。但是我希望脚本在移动到第二列之前验证 .csv 文件的整个库,即
Name, City
Joe, Orlando
Sam, Copper Town
Mike, Atlanta
所以它必须先检查列名,然后再继续检查城市?我将如何更改以下脚本来满足此要求?
# 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
我会在内循环中使用 awk
:
if awk -F, ' !~/^prefix1/ || !~ /^prefix2/ {exit(1)}' "$file" ; then
mv "$file" good
else
mv "$file" bad
fi
^prefix1
和 ^prefix2
是正则表达式模式。