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
これを修正するには?