需要验证目录中的 csv 文件列表。每列都应该通过自己的验证;因此 $1 是第一个并且它不应该为 null 等等......
csv 文件通过每列的验证后,就被认为是一个好的文件。现在我该如何提取这些好的文件,以便将它们 ftp 到另一个位置?
每次验证后我是否应该只通过 ftp 传输每个文件? (时间太多?)
创建好文件的名称列表并将其传递到一个数组中,以便我稍后可以迭代该数组以将其传递到 ftp?
这是我迄今为止的尝试。
#!/bin/sh
for file in /source/*.csv
do
awk -F',' '{
$date_regex = '~(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d~';
if (length($1) == "")
break
if (length($2) == "") && (length($2) > 30)
break
if (length($3) == "") && ($3 !~ /$date_regex/)
break
if (length($4) == "") && (($4 != "S") || ($4 != "E")
break
if (length($5) == "") && ((length($5) < 9 || (length($5) > 11)))
break
}' file
#whatever you need with "$file"
done
最佳答案
如果检查失败,则从 awk 中退出 1。当 awk 成功时使用 if 语句传输文件。
#!/bin/sh
for file in /source/*.csv; do
if awk -F',' '{
# your multiple checks here.
if (length($1) == "") && (length($2) == "") && (length($2) > 30) && ...)
exit 1
}' "$file"; then [scp or ftp command as you like]; fi
done
关于linux - shell/linux 脚本仅选择某些文件到 FTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28155831/