Search command grep under Linux

Source: Internet
Author: User

First, Introduction

grep(Global search Regular expression (RE) and print out of the line, full search of regular expressions and print out rows) is a powerful text search tool that uses regular expressions to search for text. and print out the matching lines.

Usually grep combines duct | To use, for example, to get results from the previous command through the pipeline | Pass to grep for filtering

Second, the option

-A Do not ignore binary data. -A < Displays the number of columns > displays the contents of the row, in addition to the row that conforms to the template style. -B appears outside the row that conforms to the template style and displays the contents before the line. -C calculates the number of columns that conform to the template style. -C< show columns > or-< Displays the number of columns > in addition to displaying the column that conforms to the template style, and displays the contents before the column. -D< action > You must use this parameter when you specify that you want to find a directory instead of a file, otherwise the grep command returns information and stops the action. -E< template style > Specify a string as the template style for finding the contents of a file. -E uses the template style as an extended normal notation, which means that extended regular expressions can be used. -F< template file > Specify a template file with the contents of one or more template styles so that grep finds the contents of the file that match the template criteria, formatted as a template style for each column. -F treats the template style as a list of fixed strings. -G uses the template style as a normal notation. -H does not indicate the file name that the column belongs to until it displays the column that conforms to the template style. -h indicates the file name of the column before displaying the column that conforms to the template style. The difference between the case of the-I-Hu character. -l lists file names that match the file contents to the specified template style. -l lists file names for file contents that do not conform to the specified template style. -N indicates the column number before displaying the column that conforms to the template style. -Q does not display any information. -R/R The effect of this parameter is the same as specifying the "-D recurse" parameter. -S does not display an error message. -V Reverse lookup. -W displays only the columns that match the whole word. -X displays only the columns that are eligible for all columns. -y This parameter effect is the same as "-I". -O outputs only the portion of the file that matches. 

Iii. common usage of GREP commands

Searching for a word in the file, the command returns a line of text containing "Match_pattern":

"Match_pattern" file_name

Find in multiple files:

grep "Match_pattern" File_1 file_2 file_3 ...

Output all rows except the- v option:

Grep-v "Match_pattern" file_name

Mark Match color --color=auto options:

grep "Match_pattern" file_name--color=auto

Use the regular expression- e option:

Grep-e "[1-9]+""[1-9]+"]

Output only the portion of the file that matches to the- o option:

Echo This was a test line. | Grep-o-E "[A-z]+\."] Line. Echo This was a test line. | Egrep-o "[A-z]+\.] Line.

The number of rows in the statistics file or text containing the matching string- c option:

Grep-c "Text" file_name

The output contains the number of rows that match the string- n option:

grep "text" "Text"- n#"text"-N file_1 file_2

The character or byte offset at which the plot style matches:

Echo  not Unix | Grep-b-O ' not ' 7: not
#一行中字符串的字符便宜是从该行的第一个字符开始计算, the starting value is 0. Option-b-o is always used in conjunction.

Search for multiple files and find matching text in which files:

Grep-l "text" File1 file2 file3 ...

Four, grep recursive search file

To recursively search for text in a multilevel directory:

grep "Text". -r-n#. Indicates the current directory.

Ignore character case in matching style:

Echo "Hello World" | Grep-i "Hello"Hello

Option- e brakes Multiple matching styles:

Echo This was a text line | Grep-e "is"-e "line" -oisline# You can also use the-f option to match multiple styles, writing out the characters that need to be matched line by row in the style file. Cat patfileaaabbbecho AAA bbb CCC DDD Eee | Grep-f Patfile-o

Include or exclude the specified file in the grep search results:

#只在目录中所有的. PHP and. html files recursively search for the character "main ()""main ()". -R--include *. {PHP,html}#"main ()". -R--exclude "README"# "main ()". -R--exclude-from FileList

grep with Xargswith a 0 -value byte suffix:

# Test files:echo "AAA" > file1echo "BBB" > file2echo "AAA" >  "AAA" file*-lz | xargs-0 RM#执行后会删除file1和file3, the grep output uses the-Z option to specify a 0-value byte as the Terminator file name, xargs-0 reads the input with a 0-value byte terminator separates the file name, and then deletes the matching file,- Z is usually used in conjunction with-L.

grep Silent output:

Grep-q "test" filename#不会输出任何信息, if the command runs successfully returns 0, a non-0 value is returned if it fails. Typically used for conditional testing.

Print a line before or after matching text:

#10 | grep "5"-A 35678#10 | grep "5"-B32345# 10 | grep "5"-C 32345678# If there are multiple matching results, "--" is used as the delimiter between each matching result:echo -E "A\NB\NC\NA\NB\NC" | grep a-a 1ab--ab

Above transfer from: Http://man.linuxde.net/grep

Search command grep (GO) under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.