123456789101112131415161718192021222324252627282930 |
- #!/bin/bash
- # $1:文件夹名称
- # $2:匹配字符串
- dir="/logs/$1"
- if [ ! -d "$dir" ]; then
- echo "目录 $dir 不存在"
- exit 1
- fi
- file=$(ls -t "$dir" | head -n 1)
- if [ -z "$file" ]; then
- echo "目录 $dir 中没有任何文件"
- exit 1
- fi
- matching_line=$(grep -n "$2" "$dir/$file" | tail -n 1)
- if [ -z "$matching_line" ]; then
- echo "文件 $file 中没有找到字符串 '$2'"
- exit 1
- fi
- line_number=$(echo "$matching_line" | cut -d ":" -f 1)
- echo "在文件 $file 中找到了字符串 '$2',位于第 $line_number 行:"
- sed -n "$((line_number - 30)),$((line_number + 200))p" "$dir/$file"
|