【发布时间】:2021-12-11 09:04:33
【问题描述】:
“|”分隔文件应具有以下列标题
Activity
Activity+ ID
Description
Status
上传后,在开始使用 SQLLDR 处理文件之前,我确保上传的文件具有确切数量的标题,标题名称匹配且顺序相同。
代码:
declare -i header=4
fields=(
"Activity"
"Activity+ ID"
"Description"
"Status"
)
for i in "Test File.csv"; do
read -r line < "$i"
oldIFS="$IFS"
IFS=$'|'
fldarray=( $line );
IFS="$oldIFS"
nfields=${#fldarray[@]}
if (( nfields < header ))
then
printf "error: only '%d' fields in file '%s'\nmissing:" "$nfields" "$i"
else
for item1 in "${header[@]}"; do
for item2 in "${fields[@]}"; do
if [[ $item1 != $item2 ]]; then
Array3+=("$item1")
fi
done
done
echo "not matching" ${Array3[@]}
printf "\n\n"
fi
done
数据:
Activity|Activity+ ID|Description|Status
Test|1234|First activity|Open
这总是打印缺少 Activity+ 列,尽管它存在于文件中。从上传的标题和文件中删除“+”后,它按预期工作。 如何更改上述代码以使用“+”验证列标题。 我参考了bash to identify and verify file headers 的答案来构建这个解决方案
【问题讨论】:
-
请显示文件文件的确切内容。 “应该有下面的列标题”,如果没有怎么办?还要检查你的行尾是否正常(dos2unix)。
-
我刚刚在我的系统上运行了您的代码,并且没有报告“Activity+”丢失。但我不得不猜测输入文件的格式。请添加,以便我再次查看。
-
@Nic3500 更新了问题。确切的列名是“Activity+ ID”,我现在再次执行此操作并说“Activity+ ID”缺少列标题。
-
标题可以按不同的顺序显示吗?快速而肮脏的解决方案是将第 1 行与固定字符串
Activity|Activity+ ID|Description|Status进行比较。 -
@JeffSchaller 我不能这样做,因为我需要知道哪个列标题不匹配