Grep command sorting

Source: Internet
Author: User

I. How grep works

The grep command looks for a character mode in one or more files. If this mode contains spaces, it must be enclosed by quotation marks. In the grep command, the mode can be a string enclosed by quotation marks or a single word. All words after the pattern are considered as file names. Grep sends the output to the screen without modifying or changing the input file. The exit status returned by grep is 0, indicating that the operation is successful. The exit status is 1, indicating that no result is found. If the specified file cannot be found, the exit status is 2.

2. supported regular expression metacharacters

Metacharacters Function Instance Matching result Remarks
^ First line Separator Grep "^ test" datafile Print all rows starting with test  
$ Line tail Locator Grep "Test $" datafile Print all rows ending with test  
. (Point) Match any single character except "/N" /T. T/ Match contains a t followed by two characters in the line with a T. Positioning. You only need to add an escape character.
* Matches the previous character or subexpression zero or multiple times /T * est/ Match the rows that contain 0 or more T and EST. Matching: est, test, ttest  
[] Matches any one of the group of characters. /[TT] est/ Match rows containing test or test  
[^] Match characters not in the specified group /[^ A-Z]/ Match any character that is not in the range of A to Z  
/< First-word Locator // Match the rows that contain words starting with love. Such as love and lover. Grep "/print all rows that contain words starting with North.
/> Suffix /Love/>/ Match the row containing the word ending with love.
For example, aalove
Grep "North/>" datafile
Print all rows that contain words ending with North.
/(Pattern /) Match pattern and store it in registers for later use. // (Love/) Able/1R/ A maximum of nine tags can be used. The leftmost tag in the mode is the first one. For example, the pattern love is saved as tag 1, expressed as/1. In the example on the left, the search string is a long string of loveable and lover. Sed, VI, and grep are supported.
For example:
Sed "S // (Love/) // 1 able/" function is to replace love in the file with loveable.
X/{M/} or
X/{M,/} or
X/{M, N /}
Repeated occurrence of character X;
M times, at least m times, at least m times and no more than N times.
O/{5, 10 /} Match rows that contain 5-10 consecutive letters. VI and grep support

3. grep options

Option Function
-B Add the block number before each row, which may be used to locate the disk block according to the context.
-C Displays the number of matched rows instead of the row content.
-H Do not display file name
-I Case Insensitive when comparing characters
-L Only names of the files with matching rows are listed (each file name is only listed once). The names are separated by line breaks.
-N Add the relative row number in the file before each row
-S Silent operation, that is, only the error message is displayed to check the exit status
-V Reverse lookup: Only unmatched rows are displayed.
-W Full match

Grep-n '^ South' datafile ----- option-N: add the row number before finding the row in the specified mode.

Grep-I 'pat 'datafile ----- option-I disable case sensitivity. Expression Pat contains any combination of Case sensitivity.

Grep-V 'test' datafile ----- find all rows that do not contain Test

Grep-l 'test' * ----- query and output all file names containing test in the current directory

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.