查找匹配的行並且顯示行號
ld@ubuntu:/mnt/hgfs/haShare/test$ grep 'init' -n flaskr.py
38:def init_db():
46:@app.cli.command('initdb')
47:def initdb_command():
49: init_db()
ld@ubuntu:/mnt/hgfs/haShare/test$ cat -n flaskr.py # 檢驗一下
....
在多個文件中查找
ld@ubuntu:/mnt/hgfs/haShare/test$ grep 'content' file*.txt
file1.txt:content of file 1
file1.txt:content of file 1
file1.txt:content of file 1
file1.txt:content of file 1
file2.txt:content of file 2
file2.txt:content of file 2
file2.txt:content of file 2
file2.txt:content of file 2
file3.txt:content of file 3
file3.txt:content of file 3
file3.txt:content of file 3
file3.txt:content of file 3
ld@ubuntu:/mnt/hgfs/haShare/test$ cat file*.txt # 檢驗
content of file 1
content of file 1
content of file 1
content of file 1
content of file 2
content of file 2
content of file 2
content of file 2
content of file 3
content of file 3
content of file 3
content of file 3
只顯示不匹配的行的內容
修改文件內容 在 file1.txt 中加入 hehe da
ld@ubuntu:/mnt/hgfs/haShare/test$ grep 'content' -v file*.txt
file1.txt:hehe da
ld@ubuntu:/mnt/hgfs/haShare/test$ cat file*.txt
content of file 1
content of file 1
content of file 1
content of file 1
hehe da
content of file 2
content of file 2
content of file 2
content of file 2
content of file 3
content of file 3
content of file 3
content of file 3
統計匹配的行數
ld@ubuntu:/mnt/hgfs/haShare/test$ cat file*.txt
content of file 1
hehe da
content of file 2
content of file 2
content of file 3
content of file 3
content of file 3
ld@ubuntu:/mnt/hgfs/haShare/test$ grep 'content' -c file*.txt
file1.txt:1
file2.txt:2
file3.txt:3
ld@ubuntu:/mnt/hgfs/haShare/test$
打印匹配項數
ld@ubuntu:/mnt/hgfs/haShare/test$ echo -e "1 2 3 4 5 \n hello \n 5 6" | grep -o "[0-9]" | wc -l
7
ld@ubuntu:/mnt/hgfs/haShare/test$ echo -e "1 2 3 4 5 \n hello \n 5 6" | grep -o "[0-9]"
1
2
3
4
5
5
6
使用正則表達式匹配,同時只輸出匹配部分
ld@ubuntu:/mnt/hgfs/haShare/test$ echo 'xixi: hello world!!!' | grep -o -E "\w+"
xixi
hello
world
ld@ubuntu:/mnt/hgfs/haShare/test$ echo 'xixi: hello world!!!' | grep -o -E "\w."
xi
xi
he
ll
o
wo
rl
d!
打印匹配樣式的字符偏移
ld@ubuntu:/mnt/hgfs/haShare/test$ echo gun is not unix | grep -b -o 'not'
7:not
ld@ubuntu:/mnt/hgfs/haShare/test$ echo gun is not unix | grep -b -o 'xixi'
搜索多個文件並找出匹配文本位於哪一個文件中
ld@ubuntu:/mnt/hgfs/haShare/test$ grep -l init_db *
flaskr.py
grepNote
grep: tow: Is a directory # 因爲是個目錄所以暫時搞不了 -R 可以幫忙
ld@ubuntu:/mnt/hgfs/haShare/test$
返回不匹配的文件列表
ld@ubuntu:/mnt/hgfs/haShare/test$ grep -L init_db *
file1.txt
file2.txt
file3.txt
multi_blanks
note
setup.py
grep: tow: Is a directory
tow
遞歸查找查找某個文本在哪個文件中的哪一行
# 在windows 下面裝了 cmder才能這樣使用這樣使用 shell命令
C:\Users\shan\Documents\Atom
λ grep -R -n 'grep 過濾數據'
JSON對象遍歷.md:22:grep 過濾數據
提供一個 pattern 文件來進行匹配
grep -f pattern_file source_filename
不區分大小寫
ld@ubuntu:/mnt/hgfs/haShare/test$ echo hello world | grep -i 'Hello'
hello world
ld@ubuntu:/mnt/hgfs/haShare/test$
包括或者排除,這個還有點問題,沒弄明白,會繼續更新。
--include
--exclude
出了問題,這個還需要繼續弄清楚,,,未完待續。。。。。爲什麼只解析第一個文件
ld@ubuntu:/mnt/hgfs/haShare/test$ grep "of" . -r --include *.{py,txt}
./flaskr.py: """Closes the database again at the end of the request."""
ld@ubuntu:/mnt/hgfs/haShare/test$ grep "of" . -r --include *.{txt,py}
./file1.txt:content of file 1
ld@ubuntu:/mnt/hgfs/haShare/test$ grep "of" . -R --include *.{txt,py}
./file1.txt:content of file 1
ld@ubuntu:/mnt/hgfs/haShare/test$ grep "of" . -R --include '*.{txt,py}'
ld@ubuntu:/mnt/hgfs/haShare/test$ grep "of" . -R --include *.{txt,py}
./file1.txt:content of file 1
ld@ubuntu:/mnt/hgfs/haShare/test$ grep "of" -r --include *.{txt,py}
ld@ubuntu:/mnt/hgfs/haShare/test$ grep "of" ./ -r --include *.{txt,py}
./file1.txt:content of file 1
ld@ubuntu:/mnt/hgfs/haShare/test$