欢迎光临
我们一直在努力

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

grep命令

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

格式:

grep [options] [pattern] [file]

1,匹配包含”downzz”的行

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

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

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

downzz@dev:~/linux/grep$

3,-n 添加行号

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

downzz@dev:~/linux/grep$ grep -Ev “^$|#” downzz.php
<?php
    class Person {
        public $name = ‘downzz’;
        public $age = 20;
        public function showinfo(){
            echo $this->name . PHP_EOL;
            echo $this->age. PHP_EOL;
        }
    }
downzz@dev:~/linux/grep$ cat -n downzz.php
    1    <?php
    2   
    3        class Person {
    4   
    5            #人名
    6            public $name = ‘downzz’;
    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        }

 :

本文从互联网转载,来源地址:www.downzz.com/shell/18555.html,原作者保留一切权利,若侵权或引用不当,请联系九八云(cmy.cn)删除。【九八云,优质云服务器提供商】

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。