How to use Linux to use grep command to search for text files

  

If you want to search for text files in Linux system, you can use grep command. With grep command, you can search for related keyword files, or you can search for qualified text files. Screening is a relatively common command. The following small series will introduce you to the method of using Linux to use the grep command to search for text files.

grep works like this, it searches for a string in one or more template files. If the template includes spaces, it must be referenced, and all strings after the template are treated as file names. The results of the search are sent to the standard output without affecting the contents of the original file.

grep can be used in shell scripts because grep returns the status of the search by returning a status value. If the template search succeeds, it returns 0. If the search is unsuccessful, it returns 1. If the searched file does not exist, Then return 2. We can use these return values ​​to do some automated text processing.

1. Command format:

The code is as follows:

grep [option] pattern file

2. Command Function:

Specific characters used for filtering/searching. You can use regular expressions to use a variety of commands, which is very flexible.

3. Command parameters:

-a --text #Do not ignore binary data.

-A "Show Lines" --after-context="Display Lines" #In addition to displaying the column that matches the template style, the content after the line is displayed.

-b --byte-offset # Indicates the number of the first character of the line before displaying the line that matches the style.

-B "Show Lines" --before-context="Show Lines" #In addition to displaying the line that matches the style, and display the content before the line.

-c --count #Calculate the number of columns that match the style.

-C "Display Lines" --context="Show Lines" or - "Show Lines" #In addition to displaying the line that matches the style, and before and after the line is displayed.

-d "Action" --directories=“Actions” #When you specify that you want to find a directory instead of a file, you must use this parameter, otherwise the grep command will report the information and stop the action.

-e "Template Style" --regexp="Template Style" #Specify a string as a style for finding the contents of a file.

-E --extended-regexp #Use the style as an extension of the normal notation.

-f "rule file" --file="rule file" #Specify a rule file whose content contains one or more rule styles, let grep find the contents of the file that meet the rule conditions, in the format of one per line Regular style.

-F --fixed-regexp # Treat styles as a list of fixed strings.

-G --basic-regexp #Use styles as normal representations.

-h --no-filename #Do not indicate the name of the file to which the line belongs before displaying the line that matches the style.

-H --with-filename # Indicates the name of the file to which the line belongs before displaying the line that matches the style.

-i --ignore-case #Ignore the difference in character case.

-l --file-with-matches #List the file names that match the specified style.

-L --files-without-match #Lists file names whose file content does not match the specified style.

-n --line-number # Indicates the number of columns in the row before displaying the line that matches the style.

-q --quiet or --silent #Do not display any information.

-r --recursive #The effect of this parameter is the same as the specified “-d recurse” parameter.

-s --no-messages #Do not display error messages.

-v --revert-match #Show all lines that do not contain matching text.

-V --version #Display version information.

-w --word-regexp #Show only full-character columns.

-x --line-regexp #Show only the columns that match the entire column.

-y #The effect of this parameter is the same as the specified “-i” parameter.

4. Regular expression:

Regular expression for grep:

^ #Start of anchor line For example: ‘^grep’ matches all lines starting with grep.

$ #The end of the anchor line. For example: ‘grep$’ matches all lines ending with grep.

. #match a non-newline character such as: & lsquo; gr.p & rsquo; match gr followed by an arbitrary character, then p.

* #match zero or more previous characters such as: ‘*grep’ matches all one or more spaces followed by grep.

.* #Use together to represent any character.

[] #matches a character within a specified range, such as ‘[Gg]rep’ matches Grep and grep.

[^] #match a character that is not within the specified range, such as: ‘[^A-FH-Z]rep& rsquo; matches a letter that does not contain A-R and T-Z at the beginning, followed by the rep line.

\\(..\\) #Mark matching characters, such as ‘\\(love\\)’, love is marked as 1.

\\"# Anchor the beginning of the word, such as: & lsquo; \\ "grep & rsquo; match the line containing the word beginning with grep.

\\" #The end of the anchor word, such as ‘grep\\"’ matches the line containing the word ending with grep.

x\\{m\\} # Repeat the character x, m times, such as: & lsquo;0\\{5\\}& rsquo; match the line containing 5 o.

x\\{m,\\} # Repeat the character x, at least m times, such as: & lsquo;o\\{5,\\}& rsquo; match at least 5 o lines.

x\\{m,n\\} #repeat character x, at least m times, no more than n times, such as: ‘o\\{5,10\\}’match 5-10 Line.

\\w #match text and numeric characters, that is, [A-Za-z0-9], such as: ‘G\\w*p’match with G followed by zero or more literal or numeric characters And then p.

The inverse form of \\W #\\w matches one or more non-word characters, such as period periods.

\\b #字锁符, such as: ‘\\bgrep\\b’ only matches grep.
Previous1234Next page Total 4 pages

Copyright © Windows knowledge All Rights Reserved