2011-03-15 79 views
0

的我需要添加类似文件的文本标签文件千元的目录,我使用的猫和使用添加TREC的标记格式,成千上万的文件

for file in * 
do 
cat ../gau > temp; //gau contain format i need to append in each file 
echo $file >>temp; 
cat ../gau_ >>temp ;//contains </DOCID> 
cat $file >>temp; 
cat ../gau1 >> temp; //this contain last sentence </DOC> 
cat temp > $file 
done 

它outputing到文件的流也试过,但这样做是非常缓慢的可以请告诉我一个更好和有效的方法来做到这一点。可能做到使用c .how可以我们批量打开文件,然后处理它们并放回,因为它可以固定此过程自打开和写入文件是瓶颈我想。

有没有和预制的程序(这是高效和快速)做这项工作,因为我们是在时间稀缺。

+0

请不要[cross-post](http://superuser.com/questions/257825/adding-trec-format-tags-to-thousands-of-file)。此外,如果您无法自行完成,则应请管理员链接您的帐户。 – 2011-03-15 16:19:41

回答

0

这是一个快速的Python代码,试试吧,它会执行比你的批处理脚本更快:

import os 

for dirname, dirnames, filenames in os.walk('/MY_DIRECTORY/'): 
    for filename in filenames: 
     with open(os.path.join(dirname, filename), "r+") as f: 
      str = f.read() # read everything in the file 
      f.seek(0) # rewind 
      f.write("Prepended text tags" + str) # write the new line before 
      f.close() 

我还没有尝试过,但。

0

不要cat temp > $file,只是mv temp $file - 你不需要重写该文件,只需重命名它。这肯定的糟糕表现的原因之一

for file in *; do 
    { cat ../gau; echo $file; cat ../gau_ $file ../gau1; } > temp 
    mv temp $file 
done 

你可能想选择除“GAU”,“gau_”和“gau1”更desctiptive文件名。

相关问题