2016-10-15 13 views
0

テキストファイルを取り込み、そのファイルをループし、特定の文字列が何回表示されるかを簡単なbashスクリプトで記述しようとしています。ファイル。私は最終的にこれをカスタムログサーチャーに使用したい(例えば、特定のログファイルなどの中で単語 'log in'を検索するなど)、私が比較的新しいbashには少し難しかった。私は自分の意志でさまざまな言葉で異なるログをすばやく検索し、何回起こるかを見たいと思っています。すべて私のループになるまで完全に動作します。私はgrepを間違って使っていると思いますが、それが問題なのかどうかは分かりません。私のループコードはちょっと変わったように思えるかもしれません。なぜなら私はしばらくそれをしており、常に調整しているからです。私は検索の束を行っているが、私はこの問題を抱えていた唯一の人であるように感じる(うまくいけば、それは信じられないほど簡単で、私は吸う)。すべての助けが大変ありがとうございます。Bash:ループを使ってテキストファイル内の文字列のインスタンスを数えよう

編集:私は、あなただけ一致するgrep -oを使用することができ、すべての文字列のインスタンスと

#!/bin/bash 

echo "This bash script counts the instances of a user-defined string in a file." 

echo "Enter a file to search:" 

read fileName 

echo " " 

echo $path 

if [ -f "$fileName" ] || [ -d "$fileName" ]; then 

echo "File Checker Complete: '$fileName' is a file." 
echo " " 
echo "Enter a string that you would like to count the occurances of in '$fileName'." 

read stringChoice 
echo " " 
echo "You are looking for '$stringChoice'. Counting...." 

#TRYING WITH A WHILE LOOP 
count=0 
cat $fileName | while read line 
do 
    if echo $line | grep $stringChoice; then 
     count=$[ count + 1 ] 
done 
echo "Finished processing file" 




#TRYING WITH A FOR LOOP 
# count=0 
# for i in $(cat $fileName); do 
#  echo $i 
#  if grep "$stringChoice"; then 
#   count=$[ $count + 1 ] 
#   echo $count 
#  fi 
# done 

if [ $count == 1 ] ; then 
    echo " " 
    echo "The string '$stringChoice' occurs $count time in '$fileName'." 

elif [ $count > 1 ]; then 
    echo " " 
    echo "The string '$stringChoice' occurs $count times in '$fileName'." 

fi 
elif [ ! -f "$fileName" ]; then 

echo "File does not exist, please enter the correct file name." 

fi 

答えて

0

にあなたはコード内で基本的な構文エラーをしました。また、whileループがサブシェルで実行されているため、countの変数は更新されませんでした。したがって、更新されたカウント値は決して反映されませんでした。

希望の結果を得るには、コードを次のコードに変更してください。

#!/bin/bash 

echo "This bash script counts the instances of a user-defined string in a file." 

echo "Enter a file to search:" 

read fileName 

echo " " 

echo $path 

if [ -f "$fileName" ] ; then 

     echo "File Checker Complete: '$fileName' is a file." 
     echo " " 
     echo "Enter a string that you would like to count the occurances of in '$fileName'." 

     read stringChoice 
     echo " " 
     echo "You are looking for '$stringChoice'. Counting...." 

#TRYING WITH A WHILE LOOP 
     count=0 
     while read line 
     do 
       if echo $line | grep $stringChoice; then 
         count=`expr $count + 1` 
       fi 
     done < "$fileName" 
     echo "Finished processing file" 
     echo "The string '$stringChoice' occurs $count time in '$fileName'." 

elif [ ! -f "$fileName" ]; then 
     echo "File does not exist, please enter the correct file name." 
fi 
+0

これは完璧です!まさに私が探していたもの。助けが大変ありがとうございます。 – brandnew958

3

を見つけると、文字列のすべての出現をカウントするラインごとだけではなく つのインスタンスを占めるたいです全体ではなく、ラインパイプの単語結果wc

read string; grep -o "$string" yourfile.txt | wc -l 
+1

「grep -o -c」は行の代わりにオカレンスを数えていません。 –

+0

ありがとうございます。私はループを含む答えを選ぶことを好みましたが、助けに感謝します。将来のプロジェクトでこのようにする方法に注意してください。 – brandnew958

関連する問題