[Turn] linux find text filter grep command Detailed usage of regular expressions

  grep (global search regular expression (RE) and print out the line, comprehensive search regular expression and print out the line) is a powerful text search tool, you can use a regular expression search text, and print the matching lines come out.

  The family includes Unix grep grep, egrep and fgrep. egrep and fgrep grep command with only a very small difference. grep egrep is extended to support more re metacharacters, fgrep is fixed grep or fast grep, they put all the letters as words, that is, regular expression meta-character representation back to the literal meaning of its own It is no longer special. linux using the GNU version of grep. It is more powerful and can be used by -G, -E, -F command line option egrep and fgrep of


      Filter text grep

  grep is a powerful text search tools command string to match the specified format to find files, support for regular expressions. If you do not specify any file name or file name is given, "-", the grep command to read data from the standard input device. grep family, including grep, egrep and fgrep. egrep and fgrep grep command with only a very small difference. grep egrep is extended. fixed grep or fgrep is fast grep, the command uses any regular expression meta-character representation of the literal meaning of its own, no longer special. Wherein egrep equivalent to "grep -E", fgrep equivalent to "grep -F". grep function in Linux powerful, rich support many parameters, they can easily do some text processing.

  There are at least two parameters, such as less than two parameters, grep grep will wait when used alone, until the program is interrupted. If you encounter such a situation, you can press "Ctrl + c" to terminate. By default, only search the current directory and subdirectories if recursive search, use the "r" option.

# Find a particular string in a specified file 

[root @ CentOS ~] # grep root / etc / passwd 

# pipelines combined use with 

[root @ CentOS ~] # CAT / etc / passwd | grep root 

# is displayed with content that matches line number 

[the CentOS the root @ ~] # grep -n the root / etc / the passwd 

# listen to find the line number contained in the printed nginx.conf 

[the root @ the CentOS the conf] # grep listen nginx.conf 

# used in conjunction with the pipe joint, wherein / sbin / ifconfig represents the current view of the network system configuration information, the search string contains "inetaddr", the results of the second row are looking for 

[root @ CentOS etc] # CAT filel 

[root @ CentOS etc] # grep var filel 

[root etc @CentOS] # grep -v var filel 

# show line numbers 

[root @ CentOS etc] # grep -n var filel 

[root @ CentOS nginx] # / sbin / ifconfig | grep "inet addr" 

# integrated use 

 $ grep magic / usr / src / Linux / Documentation / * | tail 

# view the contents of the file 

[root @ CentOS etc] # cat test.txt

# Specified search string, case sensitive at this time is 

[root @ CentOS etc] # grep UUID test.txt 

[root @ CentOS etc] # grep the UUID test.txt 

# specified search string is case-insensitive 

[root @ CentOS etc ] # grep -i test.txt uuid 

# list filenames matching string 

[root @ CentOS etc] # grep UUID the -l test.txt 

[root @ CentOS etc] # grep -L UUID test.txt 

# is not listed matching string filename 

[root @ CentOS etc] -L # grep uuid test.txt 

# matches the entire word 

[root @ CentOS etc] # grep -W UU test.txt 

[root @ CentOS etc] # grep UUID the Test -W .txt 

# matching except display lines, respectively, show the lines of context rows N 

[@ the CentOS the root etc] # grep -C1 the UUID test.txt 

[@ the CentOS the root etc] # grep -n -E "^ [AZ] +" test.txt 

[root @ CentOS etc] # grep -n -E "^ [^ az] +" test.txt 

# by a regular expression specified search string 

[root @ CentOS etc] # cat my.cnf

# According to regular expression search 

[root @ CentOS etc] # grep -E "datadir | socket" my.cnf 

[root @ CentOS etc] # grep mysql my.cnf 

# pipelines combined use with 

[root @ CentOS etc] # grep mysql my.cnf | grep datadir 

# recursive search 

[root @ CentOS etc] # grep -r var |. head -3

Reverse lookup file name contains test is not included in the test line

grep -v test *

Example: In the search for a matching file kkk character "test file".

[root@rhel ~]# grep 'test file' kkk test file

Example: Displays the data content comprises at least 5 contiguous aa lowercase characters in the file.

[Root @ rhel ~] # grep '[az] \ {5 \}' aa aaaaa aaaaaa

Example: Find the line at the beginning of the contents b / root / aa file.

[root@rhel ~]# grep ^b /root/aa bbb

Example: output / root / aa row of content file is not in the beginning b.

[Root @ rhel ~] # grep -v ^ b / root / aa aaaaa AAAAA BBB aaaaaa

Example: the content output line le in the end / root / kkk file.

[root@rhel ~]# grep le$ /root/kkk test file

Finding 'exception' line in the stdout1.log file.

grep 'exception' stdout1.log

Find the number of rows have 'exception' in stdout1.log file.

grep -c 'exception' stdout1.log

grep process itself does not show

ps aux | grep \ [s] sh

ps aux | grep ssh | grep -v "grep"

Output ip address

ifconfig eth0|grep -E "([0-9]{1,3}\.){3}[0-9]"

Displays the current directory to the lines that each string has a string of at least 7 consecutive lowercase .txt files ending in

grep '[a-z]\{7\}' *.txt

The log file is too big, bad view, we need to view the content they want, or get the same type of data, such as log information is not 404

grep '.' access1.log|grep -Ev '404' > access2.log

grep '.' access1.log|grep -Ev '(404|/photo/|/css/)' > access2.log

grep '.' access1.log|grep -E '404' > access2.log

grep regular expressions support a rich, common regular metacharacter meaning table

grep regular Parameter Description

 parameter  Explanation
 ^  The first match of the string specified line
 $  Specifies the end of the string matching
 *  It represents zero or more characters
 +  It represents more than one character
 \  Remove the special meaning specified characters
 ^  The beginning of the specified line
 $  The end of the specified line
 .  A non-matching character newline
 *  Match zero or more of the previous character
 []  Matching character within a specified range
 [^]  Matches one character not within the specified range
 \(..\)  Mark matching character
 <  Start specified words
 >  The end of the specified words
 X{m}  Repeating characters X, m times such as: '0 \ {5 \}' o matching comprises five rows.
 X{m,}  Repeating characters X, at least m such as: 'o \ {5, \}' o matching at least 5 rows.
 X{m,n}  Repeating characters X, at least m times, such as no more than n times: OK 'o \ {5,10 \}' o of a match 5--10.
 W  Matching text and numeric characters, i.e. [A-Za-z0-9]
 b  Word lock symbol
 +  Match one or more of the previous character
 ?  Match zero or more of the previous character
 a|b|c  Matching a or b or c
 ()  Grouping Symbol
 [: Scooping]  Alphanumeric characters
 [:alpha:]  Text characters
 [:digit:]  Numeric characters
 [:graph:]  Non-space, control characters
 [:lower:]  Lowercase characters
 [:cntrl:]  Control characters
 [:print:]  Non-null characters (including spaces)
 [:point:]  Punctuation
 [:space:]  All whitespace characters (new lines, spaces, tabs)
 [:upper:]  Uppercase characters
 [:xdigit:]  Hexadecimal digits (0-9, af, AF)
   
   

 

  Match examples:

  grep -c "48" test.txt statistics all begin with "48" character line number

  grep -i "May" test.txt case-insensitive search for all lines "May")

  grep -n "48" test.txt display line numbers; displaying the matched characters "48" and the line number of the line, in the same nl test.txt | grep 48)

  grep -v "48" test.txt no display output characters "48" all rows)

  grep "471" test.txt display line output characters "471" is located)

  grep "48;" test.txt display line to output the character "48" at the beginning, and the characters "48" is a tab key is located

  grep "48 [34]" test.txt display output with the characters "48" at the beginning, the third character is "3" or all the rows "4")

  grep "^ [^ 48]" test.txt display line is not the first line of output character "48")

  grep "[Mm]ay" test.txt 设置大小写查找:显示输出第一个字符以“M”或“m”开头,以字符“ay”结束的行)

  grep "K…D" test.txt 显示输出第一个字符是“K”,第二、三、四是任意字符,第五个字符是“D”所在的行)

  grep "[A-Z][9]D" test.txt 显示输出第一个字符的范围是“A-D”,第二个字符是“9”,第三个字符的是“D”的所有的行

  grep "[35]..1998" test.txt 显示第一个字符是3或5,第二三个字符是任意,以1998结尾的所有行

  grep "4/{2,/}" test.txt 模式出现几率查找:显示输出字符“4”至少重复出现两次的所有行

  grep "9/{3,/}" test.txt 模式出现几率查找:显示输出字符“9”至少重复出现三次的所有行

  grep "9/{2,3/}" test.txt 模式出现几率查找:显示输出字符“9”重复出现的次数在一定范围内,重复出现2次或3次所有行

  grep -n "^$" test.txt 显示输出空行的行号

  ls -l |grep "^d" 如果要查询目录列表中的目录 同:ls -d *

  ls -l |grep "^d[d]" 在一个目录中查询不包含目录的所有文件

  ls -l |grpe "^d…..x..x" 查询其他用户和用户组成员有可执行权限的目录集合

  更多的例子:

  搜索有the的行,并输出行号

  $grep -n 'the' regular_express.txt

  搜 索没有the的行,并输出行号

  $grep -nv 'the' regular_express.txt

  利 用[]搜索集合字符

  [] 表示其中的某一个字符 ,例如[ade] 表示a或d或e

  woody@xiaoc:~/tmp$ grep -n 't[ae]st' regular_express.txt

  8:I can't finish the test.

  9:Oh! the soup taste good!

  可以用^符号做[]内的前缀,表示除[]内的字符之外的字 符。

  比如搜索oo前没有g的字符串所在的行. 使用 '[^g]oo' 作搜索字符串

  woody@xiaoc:~/tmp$ grep -n '[^g]oo' regular_express.txt

  2:apple is my favorite food.

  3:Football game is not use feet only.

  18:google is the best tools for search keyword.

  19:goooooogle yes!

  内可以用范围表示,比如[a-z] 表示小写字母,[0-9] 表示0~9的数字, [A-Z] 则是大写字母们。[a-zA-Z0-9]表示所有数字与英文字符。 当然也可以配合^来排除字符。

  搜索包含数字的行

  woody@xiaoc:~/tmp$ grep -n '[0-9]' regular_express.txt

  5:However ,this dress is about $ 3183 dollars.

  15:You are the best is menu you are the no.1.

  行首与行尾字符 ^ $. ^ 表示行的开头,$表示行的结尾( 不是字符,是位置)那么‘^$’ 就表示空行,因为只有行首和行尾。

  这里^与[]里面使用的^意义不同。它表示^后面的串是在行的开头。

  比如搜索the在开头的行

  woody@xiaoc:~/tmp$ grep -n '^the' regular_express.txt

  12:the symbol '*' is represented as star.

  搜索以小写字母开头的行

  woody@xiaoc:~/tmp$ grep -n '^[a-z]' regular_express.txt

  2:apple is my favorite food.

  4:this dress doesn't fit me.

  10:motorcycle is cheap than car.

  12:the symbol '*' is represented as star.

  18:google is the best tools for search keyword.

  19:goooooogle yes!

  20:go! go! Let's go.

  woody@xiaoc:~/tmp$

  搜索开头不是英文字母的行

  woody@xiaoc:~/tmp$ grep -n '^[^a-zA-Z]' regular_express.txt

  1:"Open Source" is a good mechanism to develop programs.

  21:#I am VBird

  woody@xiaoc:~/tmp$

  $表示它前面的串是在行的结尾,比如 '/.' 表示 . 在一行的结尾

  搜索末尾是.的行

  woody@xiaoc:~/tmp$ grep -n '/.$' regular_express.txt //. 是正则表达式的特殊符号,所以要用/转义

  1:"Open Source" is a good mechanism to develop programs.

  2:apple is my favorite food.

  3:Football game is not use feet only.

  4:this dress doesn't fit me.

  5:However ,this dress is about $ 3183 dollars.

  6:GNU is free air not free beer.

  注意在MS的系统下生成的文本文件,换行会加上一个 ^M 字符。所以最后的字符会是隐藏的^M ,在处理Windows

  下面的文本时要特别注意!

  可以用cat dos_file | tr -d '/r' > unix_file 来删除^M符号。 ^M==/r

  那么'^$' 就表示只有行首行尾的空行拉!

  搜索空行

  woody@xiaoc:~/tmp$ grep -n '^$' regular_express.txt

  22:

  23:

  woody@xiaoc:~/tmp$

  搜索非空行

  woody@xiaoc:~/tmp$ grep -vn '^$' regular_express.txt

  1:"Open Source" is a good mechanism to develop programs.

  2:apple is my favorite food.

  3:Football game is not use feet only.

  4:this dress doesn't fit me.

 

  linux关系推荐:

  linux grep怎么查ip地址

  linux egrep命令在文件或标准输入中查找模式

  linux fgrep命令查找文件里符合条件的字符串

  linux rgrep命令递归查找文件里符合条件的字符串

Guess you like

Origin www.cnblogs.com/chris-oil/p/11285754.html