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 dald@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$
相关文章推荐
- windows Git Bash 无法运行python文件的解决方法
- /bin,/sbin,/usr/sbin,/usr/bin 目录之简单区别
- 写shell脚本调用php文件
- shell 的ftp简单应用
- [Headset]如何在ADB shell中读取耳机状态和读取ACCDET寄存器
- shell调用php脚本,并传递参数
- shell 学习笔记
- 脚本实现 监视ARM上某个已知名称的程序,若该程序退出了,则重新运行它。
- shell自定义函数及参数调用解析
- shell命令运行符号& ; &&区别
- shell中各种括号的作用()、(())、[]、[[]]、{}
- bash shell脚本执行的几种方法
- 文章标题
- shell中&&和||的使用方法
- linux shell 和 vim快捷键
- SSH Secure Shell Client乱码问题
- 用powershell+excel行列转置三步走
- UNIX/Linux系统管理技术手册(2)----bash脚本编程
- Powershell AWS 自动化管理 (11) - 创建一个高可用的WordPress博客(中)
- hbase shell 输入无法使用退格键删除解决办法