【发布时间】:2014-02-05 15:02:18
【问题描述】:
我有一份价值长达数年的每日 xml 报告,我正在尝试查看每一份报告并找到购买日期,并确定它是否比文件日期至少早一年。如果是这样,我将文件名和购买日期写入日志。问题是性能真的很差。
#!/bin/bash
for file in *xml ; do
fileDate=`echo ${file} | cut -c 18-35 | sed 's/.xml//'`
fileDateSeconds=`date --date="${fileDate}" +"%s"`
awk '/PurchaseDate/ {print}' ${file} >> /tmp/yamExport/tempFile.txt
cat /tmp/yamExport/tempFile.txt | while read input
do
lineDate=`echo ${input} | cut -c 15-24`
lineDateSeconds=`date --date="${lineDate}" +"%s"`
delta=`expr $fileDateSeconds - $lineDateSeconds`
if [ "$delta" -gt "31556926" ]
then
#echo "$file : $input"
echo "$file : $input" >> /tmp/yamExport/yamExportTimestamps2.log
fi
done
done
一开始我只是逐行遍历整个文件
cat ${file} | while read input
do
if [[ "$input" =~ "PurchaseDate" ]]
then
但后来我确定使用 awk 快速抓取所有带有 PurchaseDate 的行并输出到临时文件然后循环遍历会更快(但仍然很慢)。如果有人对我如何提高性能有任何建议,那将非常有帮助。我可以像循环一样对 awk 语句的输出进行操作吗?如果我能做到这一点,我认为性能会好得多。
感谢任何提示。
【问题讨论】:
标签: xml performance bash loops awk