Linux之grep搜索命令
grep命令的作用是在文件中提取和匹配符合条件的字符串行,是一种强大的文本搜索工具。他能使用正则表达式搜索文本,并把匹配的行打印出来。
命令格式
grep [选项] 搜索内容 文件名
选项
-c :只输出匹配行的计数
-i :忽略大小写
-n :输出行号
-w :显示整个单词
-R 或 -r :递归查找目录下的所有文件内容
例子
[root@localhost opt]# grep name spring-redis.xml --查找文件spring-redis.xml中所有name
[root@localhost opt]# grep -n name spring-redis.xml --查找文件spring-redis.xml中所有name添加行号
[root@localhost opt]# grep -c name spring-redis.xml --统计文件spring-redis.xml中所有name个数
[root@localhost opt]# grep -i redis spring-redis.xml --在文件spring-redis.xml中,查找redis字符忽略大小写
[root@localhost opt]# grep -w spring spring-redis.xml --单词查找spring
[root@localhost opt]# grep -r poo /opt --递归查找opt下所有含poo的内容
演示
[root@localhost opt]# grep name spring-redis.xml
<property name="maxIdle" value="0"/>
<property name="maxTotal" value="20"/>
<property name="maxWaitMillis" value="1000"/>
<property name="testOnBorrow" value="true"/>
p:host-name="192.168.236.110" p:port="6379" p:database="10" p:pool-config-ref="jedisPoolConfig">
<property name="maxInactiveIntervalInSeconds" value="6000"></property>
<property name="connectionFactory" ref="connectionFactory"/>
<property name="keySerializer" ref="stringSerializer"/>
<property name="valueSerializer" ref="genericJackson2JsonRedisSerializer"/>
<property name="hashKeySerializer" ref="stringSerializer"/>
<property name="hashValueSerializer" ref="genericJackson2JsonRedisSerializer"/>
[root@localhost opt]# grep -n name spring-redis.xml
10: <property name="maxIdle" value="0"/>
11: <property name="maxTotal" value="20"/>
12: <property name="maxWaitMillis" value="1000"/>
13: <property name="testOnBorrow" value="true"/>
18: p:host-name="192.168.236.110" p:port="6379" p:database="10" p:pool-config-ref="jedisPoolConfig">
24: <property name="maxInactiveIntervalInSeconds" value="6000"></property>
42: <property name="connectionFactory" ref="connectionFactory"/>
44: <property name="keySerializer" ref="stringSerializer"/>
46: <property name="valueSerializer" ref="genericJackson2JsonRedisSerializer"/>
48: <property name="hashKeySerializer" ref="stringSerializer"/>
49: <property name="hashValueSerializer" ref="genericJackson2JsonRedisSerializer"/>
[root@localhost opt]# grep -c name spring-redis.xml
11
[root@localhost opt]# grep -i redis spring-redis.xml
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- redisl½ԅ嗃£¬Ӏ´ώª»𰣬¶˿ڣ¬ˇ·óأ¬(usePool=trueʱ)redisµijم嗃 -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<!--ȥµis clientµÃONFIG-->
<util:constant static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/>
<bean id="stringSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<bean id="jdkSerializationRedisSerializer"
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
<bean id="genericJackson2JsonRedisSerializer"
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="valueSerializer" ref="genericJackson2JsonRedisSerializer"/>
<property name="hashValueSerializer" ref="genericJackson2JsonRedisSerializer"/>
[root@localhost opt]# grep -w spring spring-redis.xml
http://www.springframework.org/schema/beans/spring-beans.xsd
https://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- <!– Ƥ׃spring-session £¨Ƥ׃session¹²Э£©–>-->
[root@localhost opt]# grep -r poo /opt
/opt/the-match-girl.txt: In the cold and gloom a poor little girl,
/opt/the-match-girl.txt: poor little girl!
/opt/the-match-girl.txt: sat in the cold morning hours the poor girl with red cheeks and smiling mouth,
匹配到二进制文件 /opt/03 Linux CentOS7安装.pdf
/opt/spring-redis.xml: p:host-name="192.168.236.110" p:port="6379" p:database="10" p:pool-config-ref="jedisPoolConfig">
[root@localhost opt]#