首頁 > 軟體

Linux常用命令grep-從檔案或者管道中篩選匹配的行

2020-06-16 16:51:41

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


IT145.com E-mail:sddin#qq.com