【发布时间】:2013-04-24 15:30:29
【问题描述】:
我有两个文件,一个有 17k 行,另一个有 4k 行。我想将位置 115 与位置 125 与第二个文件中的每一行进行比较,如果匹配,则将第一个文件中的整行写入一个新文件。我想出了一个解决方案,我使用 'cat $filename | 读取文件。同时阅读LINE'。但大约需要 8 分钟才能完成。有没有其他方法可以像使用 'awk' 来减少这个处理时间。
我的代码
cat $filename | while read LINE
do
#read 115 to 125 and then remove trailing spaces and leading zeroes
vid=`echo "$LINE" | cut -c 115-125 | sed 's,^ *,,; s, *$,,' | sed 's/^[0]*//'`
exist=0
#match vid with entire line in id.txt
exist=`grep -x "$vid" $file_dir/id.txt | wc -l`
if [[ $exist -gt 0 ]]; then
echo "$LINE" >> $dest_dir/id.txt
fi
done
【问题讨论】:
-
使用 awk 您可以使用
NR作为行号。这样您可能会节省时间。