Linux之grep搜索命令

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">
    <!--    &lt;!&ndash; Ƥ׃spring-session £¨Ƥ׃session¹²Э£©&ndash;&gt;-->
[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]# 
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值