linux系统中的grep命令详解
grep命令是global regular expression print(全局正则表达式输出)的缩写,它是linux系统中最强大的命令之一。它在一个或者多个文件中搜索与给定的内容的匹配的行,并将匹配的内容输出。
grep语法格式:
grep [参数]
常用参数:
-i:忽略大小写
-c:只输出匹配行的数量
-l:只列出符合匹配的文件名,不列出具体的匹配行
-n:列出所有的匹配行,并显示行号
-h:查询多文件时不显示文件名
-v:显示不包含匹配文本的所有行
-w:匹配整词
-x:匹配整行
-r:递归搜索
测试目录,文件内容全部相同:

例1、文件中搜索字符串
grep test test01.txt
grep -i test test01.txt#忽略大小写

grep -n test test01.txt#-n显示行号

grep -v 003 test01.txt#-v显示不匹配的行,

例2、使用grep过滤命令输出
ps -aux|grep systemd

例3、递归输出
grep –r -h 002 ./#-r匹配当前目录所有文件,以及子目录下的所有文件,-h不显示文件名

例4、仅显示文件名
grep -l 002 ./test01.txt
grep -l -r 002 ./

例5、全词匹配
grep -w 00 test01.txt

例6、多文件使用通配符
grep test test* /home/know_action/
grep -c test test* /home/know_action/#-c显示字符串匹配的行数

grep命令有太多的用法,这里只列举了一小部分,剩余还需要大家自己挖掘。
相关推荐
-
「PHP」MVC框架是什么?为什么要用它2025-02-25 00:25:41
-
如何用PHP写一个比较安全的API系统(实现)2025-02-25 00:19:49
-
php 解析url获取相关信息2025-02-25 00:15:37
-
mysql命令总结和PyMysql2025-02-25 00:11:35
-
MySQL特性:深入理解ICP2025-02-25 00:11:07