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

Posted ghostwu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux常用基本命令:grep-从文件或者管道中筛选匹配的行相关的知识,希望对你有一定的参考价值。

grep命令

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

格式:

grep [options] [pattern] [file]

 

1,匹配包含"ghostwu"的行

[email protected]:~/linux/grep$ cat -n ghostwu.txt 
     1    my name is ghostwu
     2    how are you
     3    fine think you
     4    My name is Ghostwu
     5    whats your name?
     6    my name is ghostwu2
     7    
[email protected]:~/linux/grep$ grep "ghostwu" ghostwu.txt 
my name is ghostwu
my name is ghostwu2

 

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

[email protected]:~/linux/grep$ grep -v "ghostwu" ghostwu.txt 
how are you
fine think you
My name is Ghostwu
whats your name?

[email protected]:~/linux/grep$ 

 

3,-n 添加行号

[email protected]:~/linux/grep$ grep -n "ghostwu" ghostwu.txt 
1:my name is ghostwu
6:my name is ghostwu2
[email protected]:~/linux/grep$ grep -vn "ghostwu" ghostwu.txt 
2:how are you
3:fine think you
4:My name is Ghostwu
5:whats your name?
7:

 

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

[email protected]:~/linux/grep$ cat ghostwu.txt 
my name is ghostwu
how are you
fine think you
My name is Ghostwu
whats your name?
my name is ghostwu2

[email protected]:~/linux/grep$ grep -E "my|your" ghostwu.txt 
my name is ghostwu
whats your name?
my name is ghostwu2
[email protected]:~/linux/grep$ grep -Ev "my|your" ghostwu.txt 
how are you
fine think you
My name is Ghostwu

[email protected]:~/linux/grep$ grep -En "my|your" ghostwu.txt 
1:my name is ghostwu
5:whats your name?
6:my name is ghostwu2

 

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

[email protected]:~/linux/grep$ grep "ghostwu" ghostwu.txt 
my name is ghostwu
my name is ghostwu2
[email protected]:~/linux/grep$ grep -i "ghostwu" ghostwu.txt 
my name is ghostwu
My name is Ghostwu
my name is ghostwu2

 

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

[email protected]:~/linux/grep$ grep -c "ghostwu" ghostwu.txt 
2
[email protected]:~/linux/grep$ grep -ci "ghostwu" ghostwu.txt 
3
[email protected]:~/linux/grep$ grep -c "ghostwu" ghostwu.txt 
2
[email protected]:~/linux/grep$ grep "ghostwu" ghostwu.txt 
my name is ghostwu, nice to meet you,ghostwu
my name is ghostwu2
[email protected]:~/linux/grep$ cat -n ghostwu.txt 
     1    my name is ghostwu, nice to meet you,ghostwu
     2    how are you
     3    fine think you
     4    My name is Ghostwu
     5    whats your name?
     6    my name is ghostwu2
     7    

 

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

[email protected]:~/linux/grep$ grep -o "ghostwu" ghostwu.txt 
ghostwu
ghostwu
[email protected]:~/linux/grep$ grep -oi "ghostwu" ghostwu.txt 
ghostwu
Ghostwu
ghostwu

 

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

[email protected]:~/linux/grep$ grep -w "ghostwu" ghostwu.txt 
my name is ghostwu, nice to meet you,ghostwu
[email protected]:~/linux/grep$ grep -wi "ghostwu" ghostwu.txt 
my name is ghostwu, nice to meet you,ghostwu
My name is Ghostwu
[email protected]:~/linux/grep$ cat -n ghostwu.txt 
     1    my name is ghostwu, nice to meet you,ghostwu
     2    how are you
     3    fine think you
     4    My name is Ghostwu
     5    whats your name?
     6    my name is ghostwu2
     7    

 

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

[email protected]:~/linux/grep$ grep -Ev "^$|#" ghostwu.php
<?php
    class Person {
        public $name = ghostwu;
        public $age = 20;
        public function showinfo(){
            echo $this->name . PHP_EOL;
            echo $this->age. PHP_EOL;
        }
    }
[email protected]:~/linux/grep$ cat -n ghostwu.php 
     1    <?php
     2    
     3        class Person {
     4    
     5            #人名
     6            public $name = ghostwu;
     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        }

 

以上是关于Linux常用基本命令:grep-从文件或者管道中筛选匹配的行的主要内容,如果未能解决你的问题,请参考以下文章

grep和管道

linux 查找文件或者内容常用命令

Linux常用命令——grep

Linux中最常用的三大文本处理工具之grep

Linux常用命令——grep

Linux中重定向管道和grep命令总结