非交互式awk会丢失一些数据

时间:2016-05-12 23:00:44

标签: linux bash shell awk fflush

我在shell脚本中运行awk命令,它正在破坏数据文件。所有数据都没有加载,并且每行末尾都有换行符。

awk -v name="$filename" -v batch="$BATCHNAME" -F'|' 'BEGIN{OFS="|"}{$2=batch;print > (name".locked.concur")}' $filename.locked.concur

如果我将其作为命令运行,它就能完美运行。

我刚刚发现我可以使用fflush(),它似乎已经解决了这个问题。

所以现在我有:

awk -v name="$filename" -v batch="$BATCHNAME" -F'|' 'BEGIN{OFS="|"}{$2=batch;print > (name".locked.concur") fflush()}' $filename.locked.concur

我是否为灾难做好准备?

此外,该文件被重命名为$ filename.locked.concur0 - 我猜这个零是从fflush返回的退出代码?我如何摆脱这个0?

完整代码:

for filename in `cat ${INFILEDIR}/file_list_concur`

do

rm -f $OHAP_DAT/bad/$filename.bad

##Create Batchname based on filename

BATCH_GROUP=`ls $filename.locked.concur |cut -d '_' -f 1`
BATCH_DATE=`ls $filename.locked.concur |cut -d '_' -f 2`
BATCHNAME=${BATCH_GROUP}_Concur_${BATCH_DATE}

echo 'Batch Date = '$BATCH_DATE
echo 'Batch Group = '$BATCH_GROUP
echo 'Batch Name = '$BATCHNAME
echo 'File Name = ' $filename

gawk -v name="$filename" -v batch="$BATCHNAME" -F'|' 'BEGIN{OFS="|"}{$2=batch;print > (name".locked.concur");}' $filename.locked.concur


##End Batchname

sqlldr $ORACLE_PW control=$XXOH_TOP/bin/XXOH_AP_CONCUR_IMPORT_CTL.ctl \
              data=$OHAP_DAT/in/$filename.locked.concur \
              log=$OHAP_DAT/log/$filename.log \
              bad=$OHAP_DAT/bad/$filename.bad \
                bindsize=512000 << end_of_sqlldr
end_of_sqlldr

if [ -f $OHAP_DAT/bad/$filename.bad ]
then
  echo \!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  echo \!! 'date'
  echo \!! SQL*Loader bad file was found!!
  echo \!! Script $filename Failed - BAD FILE!
  echo \!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

1 个答案:

答案 0 :(得分:1)

问题确实是您正在读取和写入同一个文件。这应该会更好:

gawk ... print > (name".locked.concur.tmp")}' $filename.locked.concur
mv -f $filename.locked.concur.tmp $filename.locked.concur

更好的是:

gawk ... print}' $filename.locked.concur > $filename.locked.concur.tmp
mv -f $filename.locked.concur.tmp $filename.locked.concur