### grep
```bash
# with color
grep --color "pattern" file.txt
# show file name and line
grep -Hn "pattern" file.txt
# skip binary file
grep -I "pattern" file.txt
# find all java in src ang grep "getName"
find src/ -name *.java | xargs grep --color -Hn "getName" {}
#登錄成功的日期、用戶名、IP
grep "Accepted " /var/log/auth.log | awk '{print $1,$2,$3,$9,$11}'
#統計一下登錄成功的IP有哪些
grep "Accepted " /var/log/auth.log | awk '{print $11}' | sort | uniq -c | sort -nr | more
#統計了下日誌,發現大約有126254次登錄失敗的記錄,確認伺服器遭受暴力破解
grep -o "Failed password" /var/log/auth.log|uniq -c
#輸出登錄爆破的第一行和最後一行,確認爆破時間範圍:
grep "Failed password" /var/log/secure|head -1
#著色 grep
alias grep='grep -n --color=auto'
#進一步定位有哪些IP在爆破?
ipentity="(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
grep "Failed password" /var/log/secure|grep -E -o "$ipentity\.$ipentity\.$ipentity\.$ipentity"|uniq -c | sort -nr
# 匹配行上下兩行都顯示
grep -C 2 --color 'apple' file.txt
# 匹配行以下兩行都顯示
grep -A 2 --color 'apple' file.txt
# 匹配行以上兩行都顯示
grep -B 2 --color 'apple' file.txt
```