Shell basics-powerful regular grep in Linux Shell

Source: Internet
Author: User

Linux ShellStrong Regular ExpressionsGrep

Linux grep command
Use 'grep' to search for text files
If you want to find a string in several text files, you can use the 'grep' command. 'Grep' searches for the specified string in the text.
Suppose you are searching for a file with the string 'Magic 'in the'/usr/src/linux/documentation' directory:
$ Grep magic/usr/src/linux/Documentation /*
Sysrq.txt: * How do I enable the magic SysRQ key?
Sysrq.txt: * How do I use the magic SysRQ key?
The 'sysrp.txt 'file contains this string. The SysRQ function is discussed.
By default, 'grep' only searches for the current directory. If the directory contains many subdirectories, 'grep' is listed as follows:
Grep: sound: Is a directory
This may make the output of 'grep' difficult to read. There are two solutions:
Search for subdirectories: grep-r
Or ignore the subdirectory: grep-d skip
Of course, if you expect a lot of output, you can use the pipeline to transfer it to the 'less'. Read:
$ Grep magic/usr/src/linux/Documentation/* | less
In this way, you can read more conveniently.
Note that you must provide a file filtering method (* for searching all files *). If you forget, 'grep' will wait until the program is interrupted. If this happens, press and try again.
Command line parameters:
Grep-I pattern files: searches case-insensitive. Case Sensitive by default,
Grep-l pattern files: only names of matched files are listed,
Grep-L pattern files: Lists unmatched file names,
Grep-w pattern files: match only the entire word, not a part of the string (for example, match 'Magic ', not 'magical '),
Grep-C number pattern files: the matching context displays the rows of [number,
Grep pattern1 | pattern2 files: displays the rows matching pattern1 or pattern2,
Grep pattern1 files | grep pattern2: displays rows that match both pattern1 and pattern2.

Here are some special symbols used for search:
"<And"> respectively mark the start and end of a word.
For example:
Grep man * matches 'Batman ', 'manic', 'Man ', etc,
Grep '"<man' * matches 'manic 'and 'man', but not 'Batman ',
Grep '"<man">' only matches 'man ', not other strings such as 'Batman' or 'manic.
'^': Indicates the first row of the matched string, '$': indicates the end of the row of the matched string. If you are not familiar with command line parameters, try 'grep' on the graphic interface ', such as reXgrep. this software provides AND, OR, NOT, AND other syntaxes, as well as beautiful buttons. if you only need more explicit output, try fungrep.
Use find and grep to search for files in multiple directories.
# Find/-name "*. txt"-print
/: The find command searches for and searches all subdirectories from the directory /.
-Name: specify the name or name mode of the search. File ending with txt
-Print: indicates that the find command should output the file name that matches the standard.
# Find-name "*. txt"-print-exec grep test {}";
Grep test {} ";: Part of the-exec parameter. The grep command used to search for the word test is executed every time a file matches the condition specified in the-name parameter is found.
{}: The parameter tells the find command to insert the complete path and file name of the matching file each time the-exec command is executed.
";: Indicates that the"-exec "Command executed by" find "finds a matching file ends.
You can also remove-print.

Related Article

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.