grep命令 作用:從文本文件或管道數據流中篩選匹配的行及數據,配合正則表達式一起使用,功能更加強大。 格式: grep [options] [pattern] [file] 1,匹配包含"ghostwu"的行 2,-v: 不包含,相當於取反 3,-n 添加行號 4,-E,使用擴展的egrep命令, ...
grep命令
作用:從文本文件或管道數據流中篩選匹配的行及數據,配合正則表達式一起使用,功能更加強大。
格式:
grep [options] [pattern] [file]
1,匹配包含"ghostwu"的行
ghostwu@dev:~/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 what's your name? 6 my name is ghostwu2 7 ghostwu@dev:~/linux/grep$ grep "ghostwu" ghostwu.txt my name is ghostwu my name is ghostwu2
2,-v: 不包含,相當於取反
ghostwu@dev:~/linux/grep$ grep -v "ghostwu" ghostwu.txt how are you fine think you My name is Ghostwu what's your name? ghostwu@dev:~/linux/grep$
3,-n 添加行號
ghostwu@dev:~/linux/grep$ grep -n "ghostwu" ghostwu.txt 1:my name is ghostwu 6:my name is ghostwu2 ghostwu@dev:~/linux/grep$ grep -vn "ghostwu" ghostwu.txt 2:how are you 3:fine think you 4:My name is Ghostwu 5:what's your name? 7:
4,-E,使用擴展的egrep命令,模式中可以用正則表達式
ghostwu@dev:~/linux/grep$ cat ghostwu.txt my name is ghostwu how are you fine think you My name is Ghostwu what's your name? my name is ghostwu2 ghostwu@dev:~/linux/grep$ grep -E "my|your" ghostwu.txt my name is ghostwu what's your name? my name is ghostwu2 ghostwu@dev:~/linux/grep$ grep -Ev "my|your" ghostwu.txt how are you fine think you My name is Ghostwu ghostwu@dev:~/linux/grep$ grep -En "my|your" ghostwu.txt 1:my name is ghostwu 5:what's your name? 6:my name is ghostwu2
5,-i選項,不區分大小寫
ghostwu@dev:~/linux/grep$ grep "ghostwu" ghostwu.txt my name is ghostwu my name is ghostwu2 ghostwu@dev:~/linux/grep$ grep -i "ghostwu" ghostwu.txt my name is ghostwu My name is Ghostwu my name is ghostwu2
6,-c :統計匹配的行數,不是匹配字元串的次數
ghostwu@dev:~/linux/grep$ grep -c "ghostwu" ghostwu.txt 2 ghostwu@dev:~/linux/grep$ grep -ci "ghostwu" ghostwu.txt 3
ghostwu@dev:~/linux/grep$ grep -c "ghostwu" ghostwu.txt 2 ghostwu@dev:~/linux/grep$ grep "ghostwu" ghostwu.txt my name is ghostwu, nice to meet you,ghostwu my name is ghostwu2 ghostwu@dev:~/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 what's your name? 6 my name is ghostwu2 7
7,-o: 只輸出匹配到的字元串
ghostwu@dev:~/linux/grep$ grep -o "ghostwu" ghostwu.txt ghostwu ghostwu ghostwu@dev:~/linux/grep$ grep -oi "ghostwu" ghostwu.txt ghostwu Ghostwu ghostwu
8,-w: 只匹配過濾的單詞,類似於精確匹配
ghostwu@dev:~/linux/grep$ grep -w "ghostwu" ghostwu.txt my name is ghostwu, nice to meet you,ghostwu ghostwu@dev:~/linux/grep$ grep -wi "ghostwu" ghostwu.txt my name is ghostwu, nice to meet you,ghostwu My name is Ghostwu ghostwu@dev:~/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 what's your name? 6 my name is ghostwu2 7
9,常用的一招小技巧,去除文件的註釋和空行,在運維中,可以用這條命令把配置文件的空行和註釋去掉,然後用管道生成。這樣配置文件比較容易查看和配置
ghostwu@dev:~/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; } } ghostwu@dev:~/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 }