您的位置:首页 > 运维架构 > Shell

shell脚本攻略- grep 文本查找总结

2016-07-19 18:39 447 查看

查找匹配的行并且显示行号

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
co
d743
ntent 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$
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: