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

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

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$
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值