0

bash で書いたスクリプトに小さなエラーがあり、何が間違っているのかわかりません。

私はこのスクリプトを数千回の計算に使用しており、このエラーは数回 (20 回程度) しか発生していませんが、それでも発生していることに注意してください。

スクリプトが行うことは次のとおりです。基本的に、ユーティリティ w3m を使用してサイトから取得した Web ページを入力として受け取り、その中の単語のすべての出現回数をカウントします...最も一般的なものからそれらを並べ替えた後一度だけ発生する

これはコードです:

#!/bin/bash
#   counts the numbers of words from specific sites                       #
#   writes in a file the occurrences ordered from the most common         #

touch check         # file used to analyze the occurrences
touch distribution      # final file ordered

page=$1             # the web page that needs to be analyzed
occurrences=$2          # temporary file for the occurrences
dictionary=$3                       # dictionary used for another purpose (ignore this)

# write the words one by column
cat $page | tr -c [:alnum:] "\n" | sed '/^$/d' > check

# lopp to analyze the words
cat check | while read words
do
    word=${words}
    strlen=${#word}
    # ignores blacklisted words or small ones
    if ! grep -Fxq $word .blacklist && [ $strlen -gt 2 ]
    then
        # if the word isn't in the file
        if [ `egrep -c -i "^$word: " $occurrences` -eq 0 ]
        then
            echo "$word: 1" | cat >> $occurrences
        # else if it is already in the file, it calculates the occurrences
        else
            old=`awk -v words=$word -F": " '$1==words { print $2 }' $occurrences`
                    ### HERE IS THE ERROR, EITHER THE LET OR THE SED ###
            let "new=old+1"
            sed -i "s/^$word: $old$/$word: $new/g" $occurrences
        fi
    fi
done

# orders the words
awk -F": " '{print $2" "$1}' $occurrences | sort -rn | awk -F" " '{print $2": "$1}' > distribution

# ignore this, not important
grep -w "1" distribution | awk -F ":" '{print $1}' > temp_dictionary

for line in `cat temp_dictionary`
do
    if ! grep -Fxq $line $dictionary
    then
        echo $line >> $dictionary
    fi
done

rm check
rm temp_dictionary

これはエラーです: (私はそれを翻訳しているので、英語では異なる可能性があります)

./wordOccurrences line:30 let:x // where x is a number, usually 9 or 10 (but also 11, 13, etc)
1: syntax error in the espression (the error token is 1)
sed: expression -e #1, character y: command 's' not terminated // where y is another number (this one is also usually 9 or 10) with y being different from x

編集: kev と話すと、改行の問題のようです

let と sed の間にエコーを追加して sed を出力すると、エラーが発生するまで 5 ~ 10 分間完全に機能しました。通常、エラーのない sed は次のようになります。

s/^CONSULENTI: 6$/CONSULENTI: 7/g

しかし、エラーが発生したときは次のようになりました。

s/^00145: 1 1$/00145: 4/g

これを修正するには?

4

2 に答える 2

2

$old に新しい行がある場合は、awk が 2 行を出力するため、$occurences に重複があることを意味します。

このスクリプトは単語を数えるのが複雑で、多くのプロセスとプロセス ファイルをループで起動するため効率的ではないようです。多分あなたは似たようなことをすることができます

sort | uniq -c
于 2012-08-05T13:35:50.693 に答える
1

大文字と小文字を区別しないことは、プログラム全体で一貫していないことも考慮する必要があります。「foooo」だけを含むページを作成してプログラムを実行し、「Foooo」を含むページを作成してプログラムを再度実行しました。awk は大文字と小文字を区別して照合するため、'old=`awk...' 行は 'old' を空の文字列に設定します。これにより、オカレンス ファイルが更新されません。後続の sed と、場合によっては grep の一部も大文字と小文字を区別します。

これは、表示されたエラー メッセージを説明していないため、唯一のエラーではない可能性がありますが、大文字と小文字が異なる同じ単語がスクリプトによって誤って処理されることを示しています。

次の例では、単語を区切って小文字にし、3 文字未満の単語を削除します。

tr -cs '[:alnum:]' '\n' <foo | tr '[:upper:]' '[:lower:]' | egrep -v '^.{0,2}$'

スクリプトの先頭でこれを使用すると、スクリプトの残りの部分で大文字と小文字を区別する必要がなくなります。

于 2012-08-05T14:19:34.807 に答える