Linux常用命令grep-从文件或者管道中筛选匹配的行

grep命令

作用:从文本文件或管道数据流中筛选匹配的行及数据,配合正则表达式一起使用,功能更加强大。

格式:

grep [options] [pattern] [file]

1,匹配包含”linuxidc”的行

ghostwu@dev:~/linux/grep$ cat -n linuxidc.txt
  my name is linuxidc
  how are you
  fine think you
  My name is linuxidc
  what’s your name?
  my name is linuxidc2
 
ghostwu@dev:~/linux/grep$ grep “linuxidc” ghostwu.txt
my name is linuxidc
my name is linuxidc2

2,-v: 不包含,相当于取反

linuxidc@dev:~/linux/grep$ grep -v “linuxidc” ghostwu.txt
how are you
fine think you
My name is linuxidc
what’s your name?

linuxidc@dev:~/linux/grep$

3,-n 添加行号

linuxidc@dev:~/linux/grep$ grep -n “linuxidc” ghostwu.txt
1:my name is linuxidc
6:my name is linuxidc2
ghostwu@dev:~/linux/grep$ grep -vn “linuxidc” ghostwu.txt
2:how are you
3:fine think you
4:My name is linuxidc
5:what’s your name?
7:

4,-E,使用扩展的egrep命令,模式可以用正则表达式

linuxidc@dev:~/linux/grep$ cat linuxidc.txt
my name is linuxidc
how are you
fine think you
My name is linuxidc
what’s your name?
my name is linuxidc2

ghostwu@dev:~/linux/grep$ grep -E “my|your” linuxidc.txt
my name is linuxidc
what’s your name?
my name is linuxidc2
ghostwu@dev:~/linux/grep$ grep -Ev “my|your” linuxidc.txt
how are you
fine think you
My name is linuxidc

ghostwu@dev:~/linux/grep$ grep -En “my|your” linuxidc.txt
1:my name is linuxidc
5:what’s your name?
6:my name is linuxidc2

5,-i选项,不区分大小

linuxidc@dev:~/linux/grep$ grep “linuxidc” ghostwu.txt
my name is linuxidc
my name is linuxidc2
ghostwu@dev:~/linux/grep$ grep -i “linuxidc” ghostwu.txt
my name is linuxidc
My name is linuxidc
my name is linuxidc2

6,-c :统计匹配的行数,不是匹配字符串的次数

linuxidc@dev:~/linux/grep$ grep -c “linuxidc” ghostwu.txt
2
linuxidc@dev:~/linux/grep$ grep -ci “linuxidc” ghostwu.txt
3

linuxidc@dev:~/linux/grep$ grep -c “linuxidc” ghostwu.txt
2
linuxidc@dev:~/linux/grep$ grep “linuxidc” ghostwu.txt
my name is linuxidc, nice to meet you,linuxidc
my name is linuxidc2
ghostwu@dev:~/linux/grep$ cat -n linuxidc.txt
    1    my name is linuxidc, nice to meet you,linuxidc
    2    how are you
    3    fine think you
    4    My name is linuxidc
    5    what’s your name?
    6    my name is linuxidc2
    7   

7,-o: 只输出匹配到的字符串

linuxidc@dev:~/linux/grep$ grep -o “linuxidc” ghostwu.txt
linuxidc
ghostwu
linuxidc@dev:~/linux/grep$ grep -oi “linuxidc” ghostwu.txt
linuxidc
Ghostwu
linuxidc

8,-w: 只匹配过滤的单词类似于精确匹配

linuxidc@dev:~/linux/grep$ grep -w “linuxidc” ghostwu.txt
my name is linuxidc, nice to meet you,linuxidc
ghostwu@dev:~/linux/grep$ grep -wi “linuxidc” ghostwu.txt
my name is linuxidc, nice to meet you,linuxidc
My name is linuxidc
ghostwu@dev:~/linux/grep$ cat -n linuxidc.txt
    1    my name is linuxidc, nice to meet you,linuxidc
    2    how are you
    3    fine think you
    4    My name is linuxidc
    5    what’s your name?
    6    my name is linuxidc2
    7   

9,常用的一招小技巧,去除文件的注释和空行,在运维中,可以用这条命令把配置文件的空行和注释去掉,然后用管道生成。这样配置文件比较容易查看和配置

linuxidc@dev:~/linux/grep$ grep -Ev “^$|#” linuxidc.php
<?php
    class Person {
        public $name = ‘linuxidc’;
        public $age = 20;
        public function showinfo(){
            echo $this->name . PHP_EOL;
            echo $this->age. PHP_EOL;
        }
    }
linuxidc@dev:~/linux/grep$ cat -n linuxidc.php
    1    <?php
    2   
    3        class Person {
    4   
    5            #人名
    6            public $name = ‘linuxidc’;
    7   
    8            #年龄
    9            public $age = 20;
    10   
    11            #显示信息 
    12            public function showinfo(){
    13                echo $this->name . PHP_EOL;
    14                echo $this->age. PHP_EOL;
    15            }
    16        }

 本文永久更新链接地址:https://www.linuxidc.com/Linux/2018-05/152474.htm

Linux常用命令grep-从文件或者管道中筛选匹配的行

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

栗子博客 软件 Linux常用命令grep-从文件或者管道中筛选匹配的行 https://www.lizi.tw/soft/9281.html

常见问题
  • 1、杰齐1.7仅适用于PHP5.2 2、需Zend支持 3、尽量使用宝塔面板 4、尽量使用Windows 系统,关关对Linux支持不太友好。
查看详情

相关文章

评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务

Linux常用命令grep-从文件或者管道中筛选匹配的行-海报

分享本文封面