grep command
Function: Filters matching rows and data from a text file or pipeline data stream, and works with regular expressions to make them more powerful.
Format:
grep [options] [pattern] [file]
1, match the line containing "GHOSTWU"
[Email protected]:~/linux/grep$Cat-N ghostwu.txt1My name is GHOSTWU2 How is You3Fine Think you4My name is GHOSTWU5What's your name? 6My name is GHOSTWU27[email protected]:~/linux/grep$grep "GHOSTWU"ghostwu.txt My name is ghostwumy name is GHOSTWU2
2,-v: Not included, equivalent to reverse
[Email protected]:~/linux/grepgrep"ghostwu" ghostwu.txt How is Youfine think Youmy name is ghostwuwhat's your name? [email protected]:~/linux/grep
3,-n Adding line numbers
[Email protected]:~/linux/grep$grep-N"GHOSTWU"Ghostwu.txt1: My name is GHOSTWU6: My name is ghostwu2[email protected]:~/linux/grep$grep-vn"GHOSTWU"Ghostwu.txt2: How is You3: Fine Think you4: My name is GHOSTWU5: what's your name?7:
4,-E, using the Extended egrep command, you can use regular expressions in the pattern
[Email protected]:~/linux/grep$Catghostwu.txt My name is ghostwuhow be youfine think Youmy name is Ghostwuwhat's your name?My name is ghostwu2[email protected]:~/linux/grep$grep-E"My|your"ghostwu.txt My name is Ghostwuwhat's your name?My name is ghostwu2[email protected]:~/linux/grep$grep-ev"My|your"Ghostwu.txt How was youfine think Youmy name is ghostwu[email protected]:~/linux/grep$grep-en"My|your"Ghostwu.txt1: My name is GHOSTWU5: what's your name?6: My name is GHOSTWU2
5,-i option, Case insensitive
[Email protected]:~/linux/grepgrep"ghostwu" ghostwu.txt My name is Ghostwumy name is ghostwu2[email protected]:~/linux/grepgrep"ghostwu " ghostwu.txt My name is ghostwumy name was Ghostwumy name is GHOSTWU2
6,-C: Count the number of matched rows, not the number of matching strings
[Email protected]:~/linux/grepgrep"ghostwu"2 [Email protected]:~/linux/grepgrep"ghostwu" 3
[Email protected]:~/linux/grep$grep-C"GHOSTWU"Ghostwu.txt2[email protected]:~/linux/grep$grep "GHOSTWU"ghostwu.txt My name is GHOSTWU, NiceTo meet You,ghostwumy name is ghostwu2[email protected]:~/linux/grep$Cat-N ghostwu.txt1My name is GHOSTWU, NiceTo meet YOU,GHOSTWU2 How is You3Fine Think you4My name is GHOSTWU5What's your name? 6My name is GHOSTWU27
7,-o: Output only the matching string
[Email protected]:~/linux/grepgrep"ghostwu" ghostwu.txt Ghostwughostwu[email protected]:~/linux/grepgrep"ghostwu" ghostwu.txt GHOSTWUGHOSTWUGHOSTWU
8,-w: Matches only filtered words, similar to exact matches
[Email protected]:~/linux/grep$grep-W "GHOSTWU"ghostwu.txt My name is GHOSTWU, NiceTo meet You,ghostwu[email protected]:~/linux/grep$grep-wi"GHOSTWU"ghostwu.txt My name is GHOSTWU, NiceTo meet You,ghostwumy name is ghostwu[email protected]:~/linux/grep$Cat-N ghostwu.txt1My name is GHOSTWU, NiceTo meet YOU,GHOSTWU2 How is You3Fine Think you4My name is GHOSTWU5What's your name? 6My name is GHOSTWU27
9, a common trick to remove comments and blank lines, in operations, you can use this command to remove the blank lines and comments of the configuration file, and then use the pipeline to generate. This makes the configuration file easier to view and configure
[Email protected]:~/linux/grep$grep-ev"^$|#"ghostwu.php<?PHP class Person {public $name='GHOSTWU'; Public $age= -; PublicfunctionShowinfo () {Echo$thisname. Php_eol; Echo$thisAge . Php_eol; }}[email protected]:~/linux/grep$Cat-N ghostwu.php1<?PHP2 3class Person {4 5#人名6Public $name ='GHOSTWU'; 7 8#年龄9Public $age = -; Ten One#显示信息 APublicfunctionShowinfo () { - Echo$thisname. Php_eol; - Echo$thisAge . Php_eol; the } -}
Linux Common basic commands: grep-filter matching rows from a file or pipeline