grep算是一個十分常用的linux命令了,用於查找文本,是加強版的ctrl+F,下面介紹一些常用的命令參數。
1. 顯示所在行數、文件名、最大匹配個數等。
➜ ~ cat settings.json
{
"window.zoomLevel": 0,
"editor.fontSize": 14,
"breadcrumbs.enabled": true,
"editor.renderWhitespace": "none",
"terminal.integrated.rendererType": "canvas",
"terminal.integrated.fontWeightBold": "normal",
"editor.minimap.enabled": false,
"workbench.activityBar.visible": true,
"terminal.integrated.shell.osx": "/bin/zsh"
}%
cat是打印文本內容的命令,這裏不詳述,主要是爲了和grep的查找結果做對照。
- grep -n [string] [file]: 顯示[string]在[file]文件中的所在行數及內容,這裏中括號[string]表示傳入字符串,其他類似。
➜ ~ grep -n true settings.json
4: "breadcrumbs.enabled": true,
9: "workbench.activityBar.visible": true,
- grep -H [string] [file]: 顯示[string]在[file]文件中的所在文件及內容
➜ ~ grep -H true settings.json setting1.json
settings.json: "breadcrumbs.enabled": true,
settings.json: "workbench.activityBar.visible": true,
setting1.json: "breadcrumbs.enabled": true,
- grep -m [num] [string] [file]: 顯示[string]在[file]文件中出現[num]次的所在文件及內容。
➜ ~ grep -m 1 true settings.json
"breadcrumbs.enabled": true,
➜ ~ grep -m 2 true settings.json
"breadcrumbs.enabled": true,
"workbench.activityBar.visible": true,
- grep -v [string] [file]: 在[file]文件中查找不含有[string]的內容
➜ ~ grep -v true settings.json
{
"window.zoomLevel": 0,
"editor.fontSize": 14,
"editor.renderWhitespace": "none",
"terminal.integrated.rendererType": "canvas",
"terminal.integrated.fontWeightBold": "normal",
"editor.minimap.enabled": false,
"terminal.integrated.shell.osx": "/bin/zsh"
}
- grep -e [string1] -e [string2] [file]: 在[file]文件中查找含有[string1]或含有[string2]的行
➜ ~ grep -e enable -e true settings.json
"breadcrumbs.enabled": true,
"editor.minimap.enabled": false,
"workbench.activityBar.visible": true,
- grep -i: 不區分大小寫查找
- grep -w: 字符串完全匹配
- grep -C [num]: 展示匹配行上下[num]行內容
2. 文件及目錄查找限制
- grep -l [string] [files]: 在各文件[files]中展示含有[string]的文件名
- grep -L [string] [files]: 在各文件[files]中展示不含有[string]的文件名
➜ ~ grep -l true settings.json instance.csv
settings.json
➜ ~ grep -L true settings.json instance.csv
instance.csv
- grep [string] -R [dir]: 在目錄[dir]下查找含有[string]的行數
➜ ~ grep true -R setting
setting/settings.json: "breadcrumbs.enabled": true,
setting/settings.json: "workbench.activityBar.visible": true,
3. 多條件查找
- 命令可以疊加使用,如既展示文件名又展示行數,並且限制展示個數
➜ ~ grep -Hn -m 2 true setting1.json settings.json
setting1.json:4: "breadcrumbs.enabled": true,
settings.json:4: "breadcrumbs.enabled": true,
- grep -v -e [string1] -e [string2] [file] :查找既不含string1又不含string2的行內容
➜ ~ grep -v -e enable -e true settings.json
{
"window.zoomLevel": 0,
"editor.fontSize": 14,
"editor.renderWhitespace": "none",
"terminal.integrated.rendererType": "canvas",
"terminal.integrated.fontWeightBold": "normal",
"terminal.integrated.shell.osx": "/bin/zsh"
}
- grep [string1] [file] | grep [string2] :查找既含有string1又含有string2的行內容,這裏要用到通道符'|',簡單說就是'|'前面命令的輸出作爲'|'後面命令的輸入,
➜ ~ grep enable settings.json | grep true
"breadcrumbs.enabled": true,
- grep -l [string] -R [dir]: 查找目錄[dir]下含有[string]的文件名
➜ ~ grep -l true -R setting
setting/settings.json