SEARCH strings in Linux

Source: Internet
Author: User
Tags character classes control characters grep regular expression egrep

 

For example:

In the current directory, find the file with the suffix. C containing the hello string:

Find.-Name "*. c" | xargs grep-h "hello"

Appendix: (transfer)

1. grep Introduction

Grep (Global Search Regular Expression (re) and print out the line, full search for regular expressions and print out rows) is a powerful text search tool, it can use regular expressions to search for text and print matching rows. UNIX grep families include grep, egrep, and fgrep. The commands of egrep and fgrep are only slightly different from those of grep. Egrep is an extension of grep and supports more re metacharacters. fgrep is fixed grep or fast grep. They regard all the letters as words, that is, the metacharacters in a regular expression represent the literal meaning of the regular expression. They are no longer special. Linux
Use the GNU grep version. It is more powerful and can use egrep and fgrep functions through the-G,-E,-F command line options.

Grep works like this. it searches for string templates in one or more files. If the template contains spaces, it must be referenced. All strings after the template are treated as file names. The search result is sent to the screen without affecting the content of the original file.

Grep can be used in shell scripts because grep returns a status value to indicate the search status. If the template search is successful, 0 is returned. If the search is unsuccessful, 1 is returned, if the searched file does not exist, 2 is returned. We can use these return values to automate text processing.

2. grep Regular Expression metacharacter set (basic set)

For example, '^ grep' matches all rows starting with grep.

$

For example, 'grep $ 'matches all rows ending with grep.

Match a non-linefeed character, for example, 'gr. P' matches gr followed by any character and then p.

*

Match zero or multiple previous characters, such as '* grep'. Match All one or more spaces followed by grep rows. . * Represents any character.

[]

Matches a character in a specified range, for example, '[Gg] rep' matches grep and grep.

[^]

Match a character that is not within the specified range, such as '[^ A-FH-Z] rep' match a letter that does not start with the A-R and T-Z, followed by the rep line.

/(../)

Mark matching characters, such as '/(Love/)'. Love is marked as 1.

/<

Specify the start of a word, for example, '//>

Anchor specifies the end of a word. For example, 'grep/> 'matches the row containing the word ending with grep.

X/{M /}

Repeat the characters X and M, for example, '0/{5/} 'matches the rows that contain 5 o.

X/{M ,/}

Repeat character X for at least m times, for example, 'o/{5,/} 'matches rows with at least 5 o.

X/{M, N /}

Repeated characters X, at least m times, no more than N times. For example, 'o/{5, 10/} 'matches rows of 5-10 o.

/W

Match text and number characters, that is, [A-Za-z0-9], such as: 'G/W * P' match with g followed by zero or multiple characters or numbers, followed by P.

/W

/W reverse form, matching one or more non-word characters, such as periods and periods.

/B

The word lock, for example, '/bgrepb/' matches only grep.
3. Meta character extension set for egrep and grep-e

+

Matches one or more previous characters. For example, '[A-Z] + able' matches one or more lower-case letters followed by able strings, such as loveable, enable, and disable.

?

Matches zero or multiple previous characters. For example, 'gr? P' matches gr followed by one or no characters, and then the row of P.

A | B | C

Match A, B, or C. For example, grep | sed matches grep or sed.

()

Group symbols, such as: Love (able | RS) ov + matches loveable or lovers and matches one or more ov.

X {m}, X {M,}, X {m, n}

Same role as X/{M/}, X/{M,/}, X/{M, N /}

4. POSIX character class

POSIX (the Portable Operating System Interface) adds a special character class, for example [: alnum:] is another way of writing a A-Za-z0-9 to preserve one character encoding in different countries. Put them in the [] sign to become a regular expression, such as [A-Za-z0-9] or [[: alnum:]. In Linux, grep supports POSIX character classes except fgrep.

[: Alnum:]

Character

[: Alpha:]

Character

[: Digit:]

Numeric characters

[: Graph:]

Non-empty characters (non-space and control characters)

[: Lower:]

Lowercase characters

[: Cntrl:]

Control characters

[: Print:]

Non-empty characters (including spaces)

[: Punct:]

Punctuation Marks

[: Space:]

All blank characters (new line, space, Tab)

[: Upper:]

Uppercase characters

[: Xdigit:]

Hexadecimal number (0-9, A-F, A-F)

5. grep Command Options

-?

At the same time, the upper and lower lines of matching rows are displayed? Line. For example, grep-2 pattern filename simultaneously displays the upper and lower rows of matching rows.

-B, -- byte-offset

Print the block number of the row before the matching row.

-C, -- count

Only the number of matched rows is printed, and the matching content is not displayed.
-F file, -- file = File

Extract templates from files. The empty file contains 0 templates, so nothing matches.

-H, -- no-filename

When multiple files are searched, the matching file name prefix is not displayed.

-I, -- ignore-case

Ignore case differences.

-Q, -- quiet

Cancel display. Only the exit status is returned. 0 indicates that the matched row is found.

-L, -- files-with-matches

Print the list of files matching the template.

-L, -- files-without-match

Print the list of files that do not match the template.

-N, -- line-Number

Print the row number before the matched row.

-S, -- silent

The error message about the nonexistent or unreadable file is not displayed.

-V, -- revert-match

Reverse search: Only unmatched rows are displayed.

-W, -- word-Regexp

If it is referenced by/<and/>, the expression is used as a word search.

-V, -- version

Displays the software version information.

6. Instance

To make good use of the grep tool, we need to write a regular expression. Therefore, we will not explain all the functions of grep here. We will only list a few examples to illustrate how to write a regular expression.

$ LS-L | grep '^'

Filter the LS-L output content in the MPs queue and display only the rows starting with.

$ Grep 'test' D *

Display all the lines containing test in files starting with D.

$ Grep 'test' AA BB CC

The row Matching Test is displayed in the AA, BB, and CC files.

$ Grep '[A-Z]/{5/} 'aa

Display All rows of a string that contains at least five consecutive lowercase characters.

$ Grep 'W/(ES/) T. */1 'aa

If West is matched, ES is stored in the memory, marked as 1, and any characters (. *). These characters are followed by another ES (/1). If they are found, the row is displayed. If you use egrep or grep-E, you do not need to escape the "/" number and write it as 'W (ES) T. */1.

========================================================== ==========
Find the *. conf file that contains the alias string in the current directory.

Find.-Name "*. conf" | xargs grep-h "alias"
Find.-Name "*. conf"-exec grep-h "alias "{}/;

1. grep-R-l can also be implemented, but pipeline | is more efficient than simply using grep complex parameters.
2. xargs is used to expand the result obtained by find and use it as the grep parameter.

========================================================== =====
Use grep to search file content-fast and convenient

(1) Find the findcontents in the file content of all files in the current directory (case insensitive, listing the row of the file where findcontents is located) -- the number of files and directories in the current directory is relatively small. If too much content is listed after searching, it will lose the meaning of searching.
Grep-rin findcontents *

(2) If too much content is listed after searching, use the following command (only list the number of occurrences of findcontents in the file ):
Grep-ric findcontents * | grep-V: 0

(3) If you do not know the directory where the file is located, You need to search for the entire hard disk space in the root directory ), however, some files (bin, sbin, Boot, Dev, initrd, and Lib) in the root directory have no search value, so they must be excluded from the search scope.
# Cd/
Grep-rin findcontents 'ls | grep-Ve' bin | boot | Dev | initrd | lib''

------------------------------------------

-I, -- ignore-case
-N, -- line-Number
-C, -- count
-R, -- Recursive
-V, -- invert-match
-E, -- extended-Regexp
-E pattern, -- Regexp = Pattern
Use pattern as the pattern; useful to protect patterns beginning -.
Note: N becomes invalid when C exists.

========================================================== =
Grep, fgrep, and egrep commands
These commands search for files in the specified mode, notify the user to search for strings matching the specified mode, and print all text lines containing the string, the name of the row is located at the beginning of the text line. The grep command can only search for one specified mode at a time; the egrep command can search for extended regular expressions (including expression groups and options); The fgrep command can search for fixed strings without recognizing regular expressions, is a quick search command.
These commands are useful in searching and locating specific topics in files. The pattern to be searched can be considered as some keywords. You can use them to search for the keywords contained in a file. When writing a program, you can use it to find a function or related phrases. The search function of the grep command is more powerful than that of fgrep, because the search mode of the grep command can be a regular expression, but fgrep cannot. For more information about regular expressions, see the shell chapter.
Each Command in this group has a set of options, which can be used to change the output mode. For example, you can add a row number to the searched text line, output only the row number of the text line, or output all text lines that do not match the search mode, or simply output the file name that has been searched for the specified mode, and you can specify to ignore the case when searching for the mode.
This command is used to find the row that matches the pattern in the specified input file. If no file is specified, it is read from the standard input. Normally, each matched row is displayed in the standard output. If you want to find multiple files, add the file name before each line of output.
Syntax:
Grep [Option] [search mode] [file name 1, file name 2,…]
Egrep [Option] [search mode] [file name 1, file name 2,…]
Fgrep [Option] [search mode] [file name 1, file name 2,…]
The meaning of each option of this set of commands is:
-E each pattern is treated as an extended regular expression.
-F each pattern is treated as a set of fixed strings (separated by new lines), rather than as a regular expression.
-B displays the byte offset of the row that matches the string in the file before each row in the output.
-C only displays the number of matched rows.
-I is case insensitive.
-H indicates that grep should not add the file name before the output when searching for multiple files.
-L display the names of the first matching strings and separate them with line breaks. When a matching string appears multiple times in a file, the file name is not repeatedly displayed.
-N: add the row number of the matching string before the output (the row number of the first line of the file is 1 ).
-V only displays rows that do not contain matching strings.
-X only displays the rows that match the entire row.
-E expression specifies the search mode. It is used to prevent the mode starting with "-" from being interpreted as a command option.
-F expfile: Obtain the mode to be searched from the expfile file. One mode occupies one row.
Pay attention to the following aspects when using commands in this group:
Enter the search mode after the command, and then the file to be searched. You can also use special characters such as "*" in the file name list to generate a file name list. If you want to include a string with spaces in the search mode, you can enclose the search mode in single quotes to indicate that the search mode is composed of strings containing spaces. Otherwise, shell considers spaces as the delimiters of the command line parameters, while the grep command interprets words in search mode as part of the file name list. In the following example, the grep command searches for the mode "text file" in the file example ".
$ Grep 'text file' example
You can use shell special characters on the command line to generate a list of file names to be searched. In the following example, the special character "*" is used to generate a list of file names, which contains all the files in the current directory. This command is used to search for the lines matching the pattern in all files in the current directory.
$ Grep data *
Special characters are useful when searching for a specified set of objects. For example, if you want to search for all the specific modes in the C program source file, you can use "*. c" to specify the file name list. Assume that your C program contains some unnecessary goto statements. To find these statements, you can use the following command to search for and display all the code lines containing the GOTO statement:
$ Grep goto *. c
You can enter the search mode on the command line or use the-F option to read the mode to be searched from the specified file. In the file, each search mode occupies one row. This function is useful when you frequently search for a set of common strings. In the following example, if you want to search for the strings "Editor" and "CREATE" in the file exam, place the pattern to be searched in the file mypats. Then, the grep command reads the mode to be searched from the file mypats.
$ Cat mypats
Editor
Create
$ Grep-F mypats exam

File SEARCH Command
Find command
Function: Search for files in the directory structure and perform the specified operation. This command provides a lot of search conditions and is very powerful.
Syntax: Find start Directory Search Condition operation
Note: The find command starts from the specified starting directory and recursively searches for each of its subdirectories to find and perform relevant operations on the files that meet the search criteria.
The search condition provided by this command can be a composite condition composed of logical operators not, And, or. Logical operators and, or, and not have the following meanings:
(1) and: logic and is represented by "-a" in the command. It is the default option of the system. It indicates that the search condition is satisfied only when all conditions are met. For example:
$ Find ???? Ame 'tmp '???? Type C-user 'inin'
This command is used to search for all files whose conditions are met.
(2) or: logical or, represented by "-o" in the command. This operator indicates that the search condition is satisfied as long as one of the given conditions is satisfied. For example:
$ Find ???? Ame 'tmp '???? ???? Ame 'mina *'

This command is used to query all objects whose names are 'tmp 'or match 'mina.
(3) Not: Non-logical. Use "!" in the command. . This operator is used to search for objects that do not meet the given conditions. For example:
$ Find! ???? Ame 'tmp'
This command is used to query all objects whose names are not 'tmp.
Note: When many logical options are used, you can enclose these options in parentheses. To avoid shell misunderstanding, you must add the Escape Character "" before the phone number to remove the meaning of the brackets.
Example: $ find (???? Ame 'tmp '???? Type C-user 'inin ')
The search condition has the following options:
First, the N value of each of the following options can be input in three ways, assuming N is 20, then:
+ 20: After 20 (, 22, 23, etc)
-20 indicates before 20 (19,18, 17, etc)
20 indicates that the value is 20.
1. Search by name and file attributes.
-Name 'string': searches for all files whose names match the given strings. Wildcards *,? , [].
-Lname 'string': searches for all symbolic link files of the given string matching the file name. Wildcards *,? , [].
-Gid N: searches for all files belonging to the user group with ID n.
-Uid n: searches for all files belonging to users with ID n.
-Group 'string': searches for all files belonging to the string given by the user group name.
-User 'string': searches for all files belonging to the string given by the user name.
-Empty: searches for directories or files with a size of 0.
-Path 'string': searches for all files whose path names match the given strings. Wildcards *,? , [].
-Perm permission: searches for files and directories with the specified permission. The permission expression can be 711,644.
-Size N [bckw]: searches for objects of the specified file size. The character after N indicates the unit. The default value is B, which indicates the block size of 512 bytes.
-Type X: Search for objects of the X type. X is one of the following characters:
Block B Device Files
C character Device File
D directory file
P named pipe (FIFO)
F Common File
L Symbolic Links)
S socket File
-Xtype X and-type are basically the same, but only the symbolic link file is searched.
2. Search by Time
-Amin N: searches for all files that were accessed n minutes ago.
-Atime N: searches for all files that have been accessed n days ago.
-Cmin N: searches for all files whose status has been modified n minutes ago.
-Ctime N: searches for all files whose status has been modified N days ago.
-Mmin N: searches for all files whose contents have been modified n minutes ago.
-Mtime N: searches for all files whose contents have been modified N days ago.
3. executable operations
-Exec command name {} is used to execute the Linux Command for a qualified file, without asking whether the user needs to execute the command. {} Indicates that the command parameter is the file found. The end of the command must end.
-OK command name {} is used to execute the Linux Command for a qualified file. Unlike exec, it will ask whether the user needs to execute the command.
-Ls: list all the files in detail.
-The fprintf file name writes the found file name to the specified file.
-Print: the file name displayed on the standard output device.
-For how to write the printf format, see the C language.
Example 1: search for all files starting with main in the current directory and display the content of these files.
$ Find.-Name 'main * '-exec more {};
Example 2: delete a. out or *. O files not accessed in the current directory within one week.
$ Find. (-name A. Out-o-name '*. o ')
>-Atime + 7-exec RM {};
Description:
"." In the command indicates the current directory. At this time, find will start from the current directory and find the files that meet the specified conditions one by one in its subdirectories. (And) represents parentheses (), where "" is called an escape character. The reason for writing this is that (and) have different meanings for shell, rather than the purpose of combining conditions. "-Name a. Out" refers to the file named A. Out. "-name '*. o'" refers to all files whose names end with. O. The-O between the two-names indicates the logic or (OR), that is, the search name is. out or the name is. o. Find the desired file in the current directory and Its subdirectories, and then judge whether the last access time is 7 days ago (condition-atime
+ 7). If yes, run the RM (-exec RM {};) command on the file {};). {} Indicates the name of the file that meets the criteria found. It is required by the syntax. In the preceding command, the last line in the first line is the line feed. If a command is too long to be written in a row, you can enter one. Then, the system displays one>, indicating that the user can continue to enter the command.

Locate command
The locate command is used to search for files. It is faster than the find command. It requires a database, which is created by the daily routine program (crontab. After we have created this database, we can easily search for the required files.
The command is generally in the following format:
Locate
For example, search for related words issue
$ Locate issue
/Etc/issue
/Etc/issue.net
/Usr/man/man5/issue.5
/Usr/man/man5/issue.net. 5

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.