"Go" Linux in the grep command detailed __linux

Source: Internet
Author: User

A detailed explanation of GREP commands in Linuxgrep commands are very useful in Linux, and pipelines (|) Used in conjunction, very powerful for searching text files. If you want to find a string in several text files, you can use the ' grep ' command. ' grep ' searches the text for the specified string.
Suppose you are searching for a file with a 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:* I Use the Magic sysrq key?

Where the file ' Sysrp.txt ' contains the string, the SYSRQ function is discussed.

By default, ' grep ' searches only the current directory. If there are many subdirectories under this directory, ' grep ' will be listed in the following form:

Grep:sound:Is a Directory

This may make the output of ' grep ' difficult to read. Here are two ways to solve this problem:

explicitly require searching subdirectories: Grep-r
or ignores subdirectories: grep-d Skip

Of course, if you anticipate a lot of output, you can go through the pipe and transfer it to ' less ' for reading:

$ grep magic/usr/src/linux/documentation/* | Less

This way, you can read it more easily.

It is important to note that you must provide a file filtering method (*) for all files to be searched. If you forget, ' grep ' will wait until the program is interrupted. If you encounter such a situation, press <ctrl c>, and then try again.

Here are some interesting command line arguments:

grep-i pattern Files: case-insensitive search. The default case is case-sensitive,
Grep-l pattern Files: Only the matching file names are listed,
Grep-l pattern Files: Lists mismatched file names,
Grep-w pattern files: matches only the entire word, not part of the string (such as matching ' magic ', not ' magical '),
Grep-c number pattern files: matching contexts show [number] rows,
grep pattern1 | PATTERN2 files: Displays rows that match pattern1 or pattern2.
grep pattern1 Files | grep pattern2: Displays rows that match both PATTERN1 and pattern2.

Here are some special symbols for searching:

/< and/> Each mark the beginning and the end of the word.
For example:
grep man * will match ' Batman ', ' manic ', ' man ' and so on,
grep '/<man ' * matches ' manic ' and ' man ', but not ' Batman ',
grep '/<man/> ' matches only ' man ', not ' Batman ' or ' manic ' and other strings.
' ^ ': refers to the matching string at the beginning of the line,
' $ ': refers to a matching string at the end of the line,
If you are not accustomed to command-line arguments, try the ' grep ' of the graphical interface, such as Rexgrep. The software provides syntax such as and, or, not, and beautiful button:-). If you just need a clearer output, try Fungrep.

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.