背景
grep使ってよくあるタイプミスを一括で探したいな
作りました。
スクリプト
sh typo_check.sh
#!/bin/bash
# 検索対象のフォルダ
FOLDER_PATH="/{path}/"
# よくあるスペルミスのリスト (大文字小文字別々に設定)
typos=("recieve" "adress" "definately" "occured" "seperate")
# 正しいスペルのリスト (typosと対応)
correct_spelling=("receive" "address" "definitely" "occurred" "separate")
# フォルダ以下のすべてのファイルを再帰的に走査
find "$FOLDER_PATH" -type f | while read -r file; do
echo "Checking file: $file"
for i in "${!typos[@]}"; do
typo="${typos[$i]}"
correct="${correct_spelling[$i]}"
# スペルミスをgrepで検索
grep -n -H --color=always "$typo" "$file" && echo "Correct spelling should be: $correct"
done
done
txt input.txt
recieve
address
definitely
occured
seperate
実行結果
$ sh typo_check.sh
Checking file: /{path}/typo_input.txt
/{path}/typo_input.txt:1:recieve
Correct spelling should be: receive
/{path}/typo_input.txt:4:occured
Correct spelling should be: occurred
/{path}/typo_input.txt:5:seperate
Correct spelling should be: separate
※実際に実行すると、検知された単語に色が付きます。
おわりに
出来た。
チェックしたい文字列を追加してチェックしてみてください。