Вы можете проверить наличие последней строки следующим образом:
tail -1 ${filename} | egrep '^T,|^"T",' >/dev/null 2>&1
rc=$?
В этот момент $rc
будет 0, если строка начинается с T,
или "T",
, при условии, что этого достаточно, чтобы поймать запись трейлера.
Как только вы установили это, вы можете извлечь количество строк с помощью:
lc=$(cat ${filename} | wc -l)
и вы можете получить ожидаемый счетчик строк с помощью:
elc=$(tail -1 ${filename} | awk -F, '{sub(/^"/,"",$3);print 2+$3}')
и сравните их.
Так что, связав все это вместе, это было бы хорошим началом. Он выводит сам файл (мои тестовые файлы num[1-9].tst
) вместе с сообщением, указывающим, в порядке ли файл или почему он не в порядке.
#!/bin/bash
cd /exp/files
for fspec in *.tst ; do
if [[ -f ${fspec} ]] ; then
cat ${fspec} | sed 's/^/ /'
tail -1 ${fspec} | egrep '^T,|^"T",' >/dev/null 2>&1
rc=$?
if [[ ${rc} -eq 0 ]] ; then
lc=$(cat ${fspec} | wc -l)
elc=$(tail -1 ${fspec} | awk -F, '{sub(/^"/,"",$3);print 2+$3}')
if [[ ${lc} -eq ${elc} ]] ; then
echo '***' File ${fspec} is done and dusted.
else
echo '***' File ${fspec} line count mismatch: ${lc}/${elc}.
fi
else
echo '***' File ${fspec} has no valid trailer.
fi
else
ls -ald ${fspec} | sed 's/^/ /'
echo '***' File ${fspec} is not a regular file.
fi
done
Пример прогона, показывающий используемые мной тестовые файлы:
H,Test.csv,other rubbish goes here
this file does not have a trailer
*** File num1.tst has no valid trailer.
H,Test.csv,other rubbish goes here
this file does have a trailer with all quotes and correct count
"T","Test.csv","1","80045.96"
*** File num2.tst is done and dusted.
H,Test.csv,other rubbish goes here
this file does have a trailer with all quotes but bad count
"T","Test.csv","9","80045.96"
*** File num3.tst line count mismatch: 3/11.
H,Test.csv,other rubbish goes here
this file does have a trailer with all quotes except T, and correct count
T,"Test.csv","1","80045.96"
*** File num4.tst is done and dusted.
H,Test.csv,other rubbish goes here
this file does have a trailer with no quotes on T or count and correct count
T,"Test.csv",1,"80045.96"
*** File num5.tst is done and dusted.
H,Test.csv,other rubbish goes here
this file does have a traier with quotes on T only, and correct count
"T",Test.csv,1,80045.96
*** File num6.tst is done and dusted.
drwxr-xr-x+ 2 pax None 0 Feb 23 09:55 num7.tst
*** File num7.tst is not a regular file.
H,Test.csv,other rubbish goes here
this file does have a trailer with all quotes except the bad count
"T","Test.csv",8,"80045.96"
*** File num8.tst line count mismatch: 3/10.
H,Test.csv,other rubbish goes here
this file does have a trailer with no quotes and a bad count
T,Test.csv,7,80045.96
*** File num9.tst line count mismatch: 3/9.