Linux-grep command

Source: Internet
Author: User

2. Use
The grep command in a Linux system is a powerful text search tool that uses regular expressions to search for text and print matching lines. The grep full name is global Regular expression Print, which represents the globally regular expression version, and its use rights are for all users.

2. Format
grep [Options]

3. Main parameters
[Options] Main parameters:
-C: Outputs only the count of matching rows.
-I: Case insensitive (only for single-character).
-H: The file name is not displayed when querying multiple files.
-L: Only file names that contain matching characters are output when querying multiple files.
-N: Displays matching lines and line numbers.
-S: does not display error messages that do not exist or have no matching text.
-V: Displays all lines that do not contain matching text.
Pattern Regular Expression Main parameters:
\: Ignores the original meaning of special characters in regular expressions.
^: matches the start line of the regular expression.
$: Matches the end line of the regular expression.
\<: Starts from the line that matches the regular expression.
\>: End of line to match regular expression.
[]: A single character, such as [a], a meets the requirements.
[-]: range, such as [A-z], i.e. A, B, C to Z all meet the requirements.
。 : all the individual characters.
*: There are characters, the length can be 0.

4.grep command uses a simple instance
$ grep ' test ' d*
shows all the lines that contain test in a file that begins with D.
$ grep ' test ' AA bb cc
Displays the line that matches test in the aa,bb,cc file.
$ grep ' [a-z]\{5\} ' AA
Displays all rows that contain a string of at least 5 consecutive lowercase characters for each string.
$ grep ' w\ (es\) T.*\1′aa
If West is matched, es is stored in memory, labeled 1, and then searched for any character (. *) followed by another ES (\1), which is found to display the row. If you use Egrep or GREP-E, you do not have "\" number to escape, directly written as ' W (es) t.*\1′ on it.

5.grep commands use complex instances
Suppose you are searching for a file with the string ' magic ' in the '/usr/src/linux/doc ' directory:
$ grep magic/usr/src/linux/doc/*
sysrq.txt:* How do I enable the Magic SysRq key?
sysrq.txt:* How does I use the Magic sysrq key?
Where the file ' Sysrp.txt ' contains the string and discusses the functionality of SYSRQ.
By default, ' grep ' searches only the current directory. If there are many subdirectories under this directory, ' grep ' is listed in the following form:
Grep:sound:Is a Directory
This may make the output of ' grep ' difficult to read. There are two ways to solve this problem:
Explicit Requirements Search subdirectories: grep-r
or Ignore subdirectories: grep-d Skip
If you have a lot of output, you can go through the pipe to read it on ' less ':
$ grep magic/usr/src/linux/documentation/* | Less
This makes it easier for you to read.

One thing to note is that you need to provide a way to filter the file (search for all files in *). If you forget, ' grep ' will wait until the program is interrupted. If you are experiencing such a situation, press <ctrl c>, and then try again.

Here are some interesting command-line arguments:
Grep-i pattern Files: Search by case-insensitive. The default case is case-sensitive,
Grep-l pattern Files: Lists only the matching file names,
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 display [number] lines, respectively,
grep pattern1 | PATTERN2 files: Displays rows that match pattern1 or pattern2.
grep pattern1 Files | grep pattern2: Displays rows that match both PATTERN1 and pattern2.

Grep-n pattern files to display line number information

Grep-c pattern files to find total rows

Here are some special symbols for searching:
\< and \> each mark the beginning and end of the word.
For example:
grep man * will match ' Batman ', ' manic ', ' man ', etc.
grep ' \<man ' matches ' manic ' and ' man ', but not ' Batman ',
grep ' \<man\> ' matches only ' man ', not ' Batman ' or ' manic ' and other strings.
' ^ ': refers to a matching string at the beginning of the line,

' $ ': refers to a matching string at the end of the line,


Linux-grep command

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.