LoginSignup
0
0
お題は不問!Qiita Engineer Festa 2024で記事投稿!
Qiita Engineer Festa20242024年7月17日まで開催中!

特定のフォルダ以下のタイプミスを一括で検知するシェルスクリプト作った

Last updated at Posted at 2024-06-28

背景

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

※実際に実行すると、検知された単語に色が付きます。

おわりに

出来た。
チェックしたい文字列を追加してチェックしてみてください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0