0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

grepコマンドでドリルダウンするスクリプト

Posted at
#!/bin/zsh

# 使い方:
#   ./drillgrep.sh <検索ワード>
#
# 例:
#   ./drillgrep.sh TODO

if [ -z "$1" ]; then
  echo "使い方: $0 <検索ワード>"
  exit 1
fi

search_term="$1"

# ファイル一覧取得
files=($(grep -rl -- "$search_term" ./))

if [ ${#files[@]} -eq 0 ]; then
  echo "ヒットなし"
  exit 0
fi

echo "=== '${search_term}' を含むファイル一覧 ==="
for i in {1..${#files[@]}}; do
  echo "$i: ${files[$i]}"
done

# 入力待ち
echo -n "番号を選んでください (qで終了): "
read choice

if [[ "$choice" == "q" ]]; then
  echo "終了"
  exit 0
fi

if ! [[ "$choice" =~ '^[0-9]+$' ]]; then
  echo "数値を入力してください"
  exit 1
fi

if (( choice < 1 || choice > ${#files[@]} )); then
  echo "不正な番号です"
  exit 1
fi

target_file="${files[$choice]}"

echo "=== ${target_file} を検索 ==="
grep -n -- "$search_term" "$target_file"
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?