2011-02-17 20 views
1

シェルスクリプトとawkスクリプトとfindコマンドの組み合わせを使用して、何百ものファイルに複数のテキスト置換を実行しています。ファイルサイズは数百バイトから20キロバイトの間で変化します。シェルとawkスクリプトの最適化

私はこのスクリプトをスピードアップする方法を探しています。

私はcygwinを使用しています。

私はスーパーユーザーにこの質問を掲載しましたが、このフォーラムはより適切だと思います。

シェルスクリプト -

#!/bin/bash 

if [ $# = 0 ]; then 
echo "Argument expected" 
exit 1 
fi 



while [ $# -ge 1 ] 
do 
    if [ ! -f $1 ]; then 
    echo "No such file as $1" 
    exit 1 
    fi 


    awk -f ~/scripts/parse.awk $1 > ${1}.$$ 

    if [ $? != 0 ]; then 
     echo "Something went wrong with the script" 
    rm ${1}.$$ 
     exit 1 
    fi 
mv ${1}.$$ $1 
shift 
done 

awkスクリプト(簡体字) -

#! /usr/bin/awk -f 

/HHH.Web/{ 
    if (index($0,"Email") == 0) { 
     sub(/HHH.Web/,"HHH.Web.Email"); 
    } 
    printf("%s\r\n",$0); 
    next; 
} 

find . -type f | xargs ~/scripts/run_parser.sh 
+1

ない[クロスポスト](http://superuser.com/questions/246725/optimize-shell-and-awk-script)を行ってください。 –

答えて

2
find . -type f | while read -r file 
do 
    awk '/HHH.Web/ && !/Email/ { 
    sub(/HHH.Web/,"HHH.Web.Email"); 
    printf("%s\r\n",$0); 
    next; 
    } 
    ' "$file" > ${file}.$$ && mv ${file}.$$ "$file" 
done 

はあなたが意志指定されたファイルを知っていれば、コマンドラインプロセスであれば、を追加することができますオプション

+0

ありがとうございました。しかし、それは速くはありません。いくつかの簡単なテストに基づいて、そのスクリプトは数パーセント遅くなります。 – Bryan

1

あなたは各ファイルに対して新しいawkプロセスを生成しています。私はファイル(私が知っているではない一般的な方法)のための場所awk_script.awkチェック

find . -type f | xargs ./awk_script.awk 

のようなものを持つことが可能であると思うだろう。おそらくmv $ {f}。$$ $ fも行うことができましたが、bashとは別のパスとして実行できます。

これが役に立ちます。

+0

ありがとう、私はそれを見た[リンク](http://superuser.com/questions/246725/optimize-shell-and-awk-script/247275#247275) – Bryan

2

Cygwin上では、fork() - exec()を可能な限り避けることが最も重要です。 Windowsは、desginによって、linuxのような複数のプロセスを扱うようには構築されていません。 fork()がなく、牛が壊れています。 したがって、スクリプトを書くときは、できるだけ1つのプロセスから実行してみてください。

この場合、awkと1つのawkだけが必要です。すべてのコストでxargsを避けてください。 もう1つのことは、複数のファイルをスキャンする必要がある場合、Windowsのディスクキャッシュはちょうどジョークです。代わりに、すべてのファイルをアクセスする は、より良いアプローチは、あなたが開く必要がありgrepのは ので、あなたが

grep -r "some-pattern-prahaps-HHH.Web-or-so" "/dir/to/where/you/have/millions/of/files/" |awk -f ~/scripts/parse.awk 

そして「〜/スクリプト/ parse.awk」内だろう要件 を与え一致するファイルだけを見つけるようにすることですawk内のファイルをclose()して、処理速度を上げてください。 できるだけsystem()を使用しないでください。

#!/bin/awk 
BEGIN{ 
    id=PROCINFO["pid"]; 
} 
# int staticlibs_codesize_grep(option, regexp, filepath, returnArray, returnArray_linenum ) 
# small code size 
# Code size is choosen instead of speed. Search may be slow on large files 
# "-n" option supported 
function staticlibs_codesize_grep(o, re, p, B, C, this, r, v, c){ 
if(c=o~"-n")C[0]=0;B[0]=0;while((getline r<p)>0){if(o~"-o"){while(match(r,re)){ 
B[B[0]+=1]=substr(r,RSTART,RLENGTH);r=substr(r,RSTART+RLENGTH);if(c)C[C[0]+=1]=c;} 
}else{if(!match(r,re)!=!(o~"-v")){B[B[0]+=1]=r;if(c)C[C[0]+=1]=c;}}c++}return B[0]} 
# Total: 293 byte , Codesize: > 276 byte, Depend: 0 byte 

{ 
    file = $0; 
    outfile = $0"."id; # Whatever. 
    # If you have multiple replacements, or multiline replacements, 
    # be carefull in the order you replace. writing a k-map for efficient condition branch is a must. 
    # Also, try to unroll the loop. 

    # The unrolling can be anyting, this is a trade between code size for speed. 
    # Here is a example of a unrolled loop 
    # instead of having while((getline r<file)>0){if(file~html){print "foo";}else{print "bar";};}; 
    # we have moved the condition outside of the while() loop. 
    if(file~".htm$"){ 
     while((getline r<file)>0){ 
      # Try to perform minimum replacement required for given file. 
      # Try to avoid branching by if(){}else{} if you are inside a loop. 
      # Keep it minimalit and small. 
      print "foo" > outfile; 
     } 
    }else{ 
     while((getline r<file)>0){ 
      # Here, as a example, we unrolled the loop into two, one for htm files, one for other files. 
      print "bar" > outfile; 
      # if a condition is required, match() is better 
      if(match(r,"some-pattern-you-want-to-match")){ 
       # do whatever complex replacement you want. We reuse the RSTART,RLENGTH from match() 
       before_match = substr(r,1,RSTART); 
       matched_data = substr(r,RSTART,RLENGTH); 
       after_match = substr(r,1,RSTART+RLENGTH); 
       # if you want further matches, like grep -o, extracting only the match 
       a=r; 
       while(match(a,re)){ 
        B[B[0]+=1]=substr(a,RSTART,RLENGTH); 
        a=substr(a,RSTART+RLENGTH); 
       } 
       # Avobe stores multiple matches from a single line, into B 
      } 
      # If you want to perform even further complex matches. try the grep() option. 
      # staticlibs_codesize_grep() handles -o , -n , -v options. It sould satisfy most of the daily needs. 
      # for a grep-like output, use printf("%4s\t\b:%s\n", returnArray_linenum[index] , returnArray[index]); 

      # Example of multiple matches, against data that may or may not been replaced by the previous cond. 
      if(match(r,"another-pattern-you-want-to-match")){ 
       # whatever 
       # if you decide that replaceing is not good, you can abort 
       if(for_whatever_reason_we_want_to_abort){ 
        break; 
       } 
      } 
      # notice that we always need to output a line. 
      print r > outfile; 
     } 
    } 
    # If we forget to close file, we will run out of FD 
    close(file); 
    close(outfile); 
    # now we can move the file, however I would not do it here. 
    # The reason is, system() is a very heavy operation, and second is our replacement may be imcomplete, by human error. 
    # system("mv \""outfile"\" \""file"\" ") 
    # I would advice output to another file, for later move by bash or any other shell with builtin mv command. 
    # NOTE[*1] 
    print "mv \""outfile"\" \""file"\" " > "files.to.update.list"; 
} 
END{ 
    # Assuming we are all good, we should have a log file that records what has been modified 
    close("files.to.update.list"); 
} 

# Now when all is ready, meaning you have checked the result and it is what you desire, perform 
# source "files.to.update.list" 
# inside a terminal , or 
# cat "files.to.update.list" |bash 
# and you are done 
# NOTE[*1] if you have file names containing \x27 in them, the escape with \x22 is incomplete. 
# Always check "files.to.update.list" for \x27 to avoid problems 
# prahaps 
# grep -v -- "`echo -ne "\x27"`" > "files.to.update.list.safe" 
# then 
# grep -- "`echo -ne "\x27"`" > "files.to.update.list.unsafe" 
# may be a good idea.