grep هو أداة البحث اليومية على كل Unix. الصيغة الأساسية "أرني الأسطر التي تطابق هذا النمط" لكن مجموعة الأعلام (recursive, case-insensitive, fixed-string, سياق) تجعله مفيدًا.
المتطلبات المسبقة
- shell على Linux/macOS/BSD.
الخطوة 1: المطابقة الافتراضية
grep ERROR /var/log/syslog
grep -i error /var/log/syslog
grep -F "1.2.3.4"
grep -w error
grep -v "^#"
grep -c ERROR /var/log/syslog
الخطوة 2: بحث recursive
grep -r "TODO" .
grep -rn "TODO" .
grep -rl "TODO" .
grep -r --include='*.php' "TODO" .
grep -r --exclude-dir='node_modules' --exclude-dir='.git' "TODO" .
الخطوة 3: أسطر السياق
grep -A 3 ERROR app.log
grep -B 3 ERROR app.log
grep -C 3 ERROR app.log
الخطوة 4: regex موسع
grep -E '" (4|5)[0-9]{2} ' access.log
grep -E '([0-9]{1,3}\.){3}[0-9]{1,3}' file.txt
grep -Eo '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}' contacts.txt
الخطوة 5: أنماط متعددة
grep -e ERROR -e CRITICAL -e FATAL app.log
grep -E 'ERROR|CRITICAL|FATAL' app.log
grep -f patterns.txt app.log
الخطوة 6: مع find
find . -name '*.php' -mtime -7 -exec grep -Hn 'DB_PASSWORD' {} +
git ls-files '*.php' | xargs grep -ln '@deprecated'
الخطوة 7: ripgrep (rg)
sudo apt install -y ripgrep
rg TODO
rg -t php "TODO"
rg -e ERROR -A 3 -B 1
الخطوة 8: سطور مفيدة
grep -oE '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' /var/log/nginx/access.log \
| sort | uniq -c | sort -nr | head -20
grep -rIc "TODO" . | grep -v ':0$' | sort -t: -k2 -nr | head -20
التحقق
grep --version | head -1
echo -e "alpha\nbeta\ngamma" | grep -c beta
الخاتمة
grep -rIn --include='*.X' PATTERN . يغطي 80% من احتياجات البحث.
Comments
0 total · 0 threads