Linux-grep命令
概述
grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。
语法
grep命令常见用法
在文件中搜索一个单词,命令会返回一个包含“match_pattern”的文本行
grep match_pattern file_name
grep "match_pattern" file_name
在多个文件中查找
grep "match_pattern" file1 file2 ...
输出除匹配项之外的所有行 -v 选项
grep -v "match_pattern" file_name
标记匹配颜色 –color=auto 选项
grep "match_pattern" file_name --color=auto
使用正则表达式 -E 选项
grep -E "[1-9]+"
或
egrep "[1-9]+"
只输出文件中匹配到的部分 -o 选项
[root@entel2 test]# echo this is a test line. | grep -o -E "[a-z]+\."
line.[root@entel2 test]# echo this is a test line. | egrep -o "[a-z]+\."
line.
统计文件或者文本中包含匹配字符串的行数 -c 选项
[root@entel2 test]# cat args.txt
aaa
bbb
ccc
bbb
ddd
aaaaa
[root@entel2 test]# grep -c "aaa" args.txt
2
输出包含匹配字符串的行数 -n 选项
[root@entel2 test]# grep -n "aaa" args.txt
1:aaa
6:aaaaa
[root@entel2 test]# cat args.txt |grep -n "aaa"
1:aaa
6:aaaaa#多个文件
grep "match pattern" -n file_1 file_2
打印样式匹配所位于的字符或字节偏移
[root@entel2 test]# echo gun is not unix | grep -b -o "not"
7:not
[root@entel2 test]#
[root@entel2 test]# echo gun is not unix | grep -b -o "is"
4:is
[root@entel2 test]# echo gun is not unix | grep -b -o "gun"
0:gun
[root@entel2 test]# echo gun is not unix | grep -b -o "i"
4:i
13:i
一行中字符串的字符便宜是从该行的第一个字符开始计算,起始值为0。
选项 -b -o 一般总是配合使用。
搜索多个文件并查找匹配文本在哪些文件中
[root@entel2 test]# cat grep1.txt
xiaogongjiang
xiaogongjiang1
[root@entel2 test]# cat grep2.txt
xiaogongjiang
this is just for test grep -l
[root@entel2 test]# grep -l "xiaogongjiang" grep1.txt grep2.txt
grep1.txt
grep2.txt
[root@entel2 test]# grep -l "xiaogongjiang" grep*
grep1.txt
grep2.txt
[root@entel2 test]# grep -l "test" grep*
grep2.txt
[root@entel2 test]#
grep递归搜索文件
在多级目录中对文本进行递归搜索
# .表示当前目录。
[root@entel2 ~]# grep "xiaogongjiang" . -n -r
./test/grep1.txt:1:xiaogongjiang
./test/grep1.txt:2:xiaogongjiang1
./test/grep2.txt:1:xiaogongjiang
忽略匹配样式中的字符大小写
[root@entel2 ~]# echo "hello world" | grep -i "HELLO"
hello world
选项 -e 制动多个匹配样式
[root@entel2 ~]# echo this is a text line | grep -e "is" -e "line" -o
is
is
line
在grep搜索结果中包括或者排除指定文件
使用0值字节后缀的grep与xargs
grep静默输出
打印出匹配文本之前或者之后的行
Linux-grep命令相关推荐
- linux grep命令总结
风生水起 善战者,求之于势,不责于人,故能择人而任势. 博客园 首页 新随笔 联系 订阅 管理 posts - 791, comments - 394, trackb ...
- Linux grep 命令初步学习
Linux grep 命令用于查找文件里符合条件的字符串. grep 指令用于查找内容包含指定的范本样式的文件,如果发现某文件的内容符合所指定的范本样式,预设 grep 指令会把含有范本样式的那一列显 ...
- Linux grep命令、Linux cd命令、Linux pwd命令
前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. Linux grep命令用于查找文件里符合条件的字符串. grep指令用于查找内容包含指定的范本样式 ...
- linux grep命令参数及用法详解---linux管道命令grep
功能说明:查找文件里符合条件的字符串. 语 法:grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数>] ...
- linux grep命令例子,详解linux grep命令
本文介绍了linux grep命令,具体如下: 1.作用 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regu ...
- linux grep命令详解
简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...
- [转]linux grep命令
转自:http://www.cnblogs.com/end/archive/2012/02/21/2360965.html 1.作用 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则 ...
- Linux grep命令分析以及C语言版本的实现
1.作用 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全 ...
- Linux Grep 命令说明
一. Grep 说明 grep (global search regular expression(RE) and print out the line)是一种强大的文本搜索工具,它能使用正则表达式搜 ...
- linux grep命令详解_Linux 上USB 调试神器lsusb命令详解
大家好, 我是吉阿, 今天给大家介绍Linux上USB调试工具lsusb命令. USB,是英文Universal Serial Bus(通用串行总线)的缩写,是一个外部总线标准,早期用于规范电脑与 ...
最新文章
- 关于学外语,这些人人都知道的“常识”,可能是错的……
- Altium designer关于铺铜的操作
- [转]Linux awk 命令 说明
- OpenGL学习笔记以及其它学习思考
- vue脚手架创建项目步骤
- mysql order by基于时间的盲注
- js php 正则差别,JavaScript正则表达式的浏览器的差异
- 组合式应用新利器?SaaS新时代事件网格如何解决集成标准化问题
- 台积电5nm生产线污染原因查明:不影响A15芯片量产
- 丹琦女神的对比学习新SOTA,在中文表现如何?我们补充实验后,惊了!
- PHP 模拟库存出库商品
- UliPad源码+Anaconda+wxPython环境配置
- 使用C++刷算法题的简明教程
- ckeditor使用
- 怎么从已有文件中挑选需要的文字重新生成新文件_word 中如何实现 一个正文位置输入 另一个正文地方自动生成一样的...
- 倍福PLC和C#通过ADS通信传输bool类型变量
- 【java之汉字转拼音】
- 前端CDN资源库,解决HTML大屏首次加载慢的问题了,大屏项目必备cdn加速
- java long 和long_java中Long和long的区别
- 2019年10月8日股市走势预测
热门文章
- CCF 202104-4 校门外的树 Python
- C++用顶层函数重载操作符(一)
- leetcode -python 三数之和原创
- sql 精读(六)获取每个类别中最受欢迎的商品
- C++ 指向指针的指针(多级间接寻址)
- 文计笔记2: 计算机硬件知识
- Tableau实战系列浏览 Tableau 环境(一) -初识tableau操作界面
- 产品经理必知必会知识-竞品分析需求实战报告
- c++求解自行车慢速比赛问题_一元一次方程应用专题,十大题型(包括数轴上动点问题)...
- LeetCode-动态规划基础题-509. 斐波那契数