2011-03-15 1 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 

을 사용하여 파일의 스트림에 출력 (outputting) 시도했지만 이 일을 아주 천천히 할 수 있습니다 제발 할 수있는 더 나은 효율적인 방법을 말해 주시기 바랍니다. C를 사용하여 할 수 있습니다. 어떻게 우리가 일괄 파일을 열고 그들을 처리하고 다시 시작하고 이후이 프로세스를 고정시킬 수 넣어 파일은 병목 목이라고 생각합니다.

우리는 시간이 부족하기 때문에 효율적이고 빠른이 일을 할 수있는 프로그램이 있습니까?

+0

[교차 게시] (http://superuser.com/questions/257825/adding-trec-format-tags-to-thousands-of-file)를 사용하지 마십시오. 또한 직접 할 수 없다면 운영자에게 계정 연결을 요청해야합니다. –

답변

0

이 빠른 파이썬 코드는, 그것을 시도, 그것은 당신의 배치 스크립트 실행 속도가 빠른 것 : 그래도 난 그것을 시도하지 않은

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 파일 이름을 선택 할 수 있습니다.

관련 문제