Linux: linuxgrep usage

Source: Internet
Author: User
Tags egrep
Linux: linuxgrep usage [Linux overview] grep -- printlinesmatchingapattern (list the rows that match the style) ◎ syntax: grep [opt... linux learning path: linux grep usage [Linux first glance] grep -- print lines matching a pattern (list the rows that match the style) grep -- print lines matching a pattern (list the rows that match the style) ◎ syntax: grep [options] PATTERN [FILE...] grep is used to compare the corresponding part in the file, or when no file is specified, it is compared by the standard input. By default, grep lists the rows that match the style. In addition, there are two programs: grep variant, egrep and fgrep. Egrep is equivalent to grep-E, and fgrep is equivalent to grep-F. ◎ Parameter 1.-a num, -- after-context = NUM in addition to listing the row that conforms to the rule and listing the row that follows the NUM. Ex: $ grep-A 1 panda file (search for rows with A panda style from the file and display the last row of the row) 2. -a or -- text grep was originally used to search for text files. if a Binary file is used as the search target, the following message is displayed: Binary file name matches and then ends. If the-a parameter is added, the binary file can be viewed as a text file search, which is equivalent to the -- binary-files = text parameter. Ex: (searches for the panda style from the binary file mv) (error !!!) $ Grep panda mv Binary file mv matches (this indicates that this file has a match. for details, see -- binary-files = TYPE) $ (correct !!!) $ Grep-a panda mv 3. -B NUM, -- before-context = NUM is opposite to-A NUM, but this parameter displays the NUM row before it except for the row that conforms to it. Ex: (search for rows with a panda style from the file and display the first row of the row) $ grep-B 1 panda file 4. -C [NUM],-NUM, -- context [= NUM]: lists the upper and lower NUM rows that match the rows and the upper and lower NUM rows. the default value is 2. Ex: (list all rows in a file including the panda style and the upper and lower lines) (if you want to change the default value, simply change NUM) $ grep-C [NUM] panda file 5. -B, -- byte-offset lists the total number of bytes in the text before the style .. ex: $ grep-B panda file: The result is similar to 0: panda 66: pandahuang 123: panda03 6. -- binary-files = TYPE this parameter TYPE is set to binary by default. if you search in normal mode, there are only two results: 1. if yes: displays the Binary file name matches 2. if there is no match: nothing is displayed. If the TYPE is without-match, grep considers that the binary file does not contain any search style, which is the same as the-I parameter. If TPYE is text, grep regards the binary file as a text file, which is the same as the-a parameter. Warning: -- binary-files = text if the output is a terminal, unnecessary output may be generated. 7.-c, -- count does not display the rows that match the style, but only displays the total number of rows that match the style. If-v and -- invert-match are added, the total number of non-conforming rows is displayed. 8.-d ACTION, -- directories = ACTION if the input file is a folder, use ACTION to process the folder. The default ACTION is read (read), which means the folder is considered as a normal file. if the ACTION is a skip (skipped), the folder will be skipped by grep: if the ACTION is recurse (recursive ), grep reads all files in the folder, which is equivalent to the-r parameter. 9.-E, -- extended-regexp uses the regular expression to explain the style. 10.-e PATTERN, -- regexp = PATTERN the style is used as a partern, which is usually used to avoid the use of-to start with partern. 11.-f FILE, -- file = FILE write the style to be searched in advance to an archive with one style in one row. Then, archive search is used. An empty file indicates that no style is to be searched, so there will be no matching. Ex: (newfile is the search style file) $ grep-f newfile file 12.-G, -- basic-regexp treats the style as a basic rule expression explanation. (This is the default) 13.-H, -- with-filename add the name of the file that matches the style line. if there is a path, the path is displayed. Ex: (search for the panda style in file and testfile) $ grep-H panda file. /testfile file: panda. /testfile: panda $14. -h, -- no-filename is similar to the-H parameter, but the path is not displayed in the output. 15. -- help generates a short help message. 16.-I grep will force it to think that this binary file does not contain any search style, which is the same as the -- binary-files = without-match parameter. Ex: $ grep-I panda mv 17.-I, -- ignore-case is case-insensitive, including the style to be searched and the file to be searched. Ex: $ grep-I panda mv 18.-L, -- files-without-match does not display common output results, but shows non-conforming file names. 19.-l, -- files-with-matches does not display common output results. only the names of the matching files are displayed. 20. -- if possible, use mmap system call to read the input, rather than the preset read system call. In some situations, -- mmap can produce better performance. However, -- mmap may cause undefined behavior (including core dump) if the file is shortened during operation or an I/O error occurs ),. 21.-n, -- line-number: Mark the row number before the row is displayed. Ex: $ grep-n panda file: The result is similar to the following: row number: the row content 22.-q, -- quiet, -- silent does not display any general output. See-s or -- no-messages 23.-r, -- recursive recursively read all files in each folder, which is equivalent to the-d recsuse parameter. 24.-s, -- no-messages do not display error messages about nonexistent or unreadable objects. Note: Unlike GNU grep, the traditional grep does not comply with the POSIX.2 protocol, because the-q parameter is missing and its-s parameter table is like the-q parameter of GNU grep. Shell scripts tend to Port traditional grep, avoid-q and-s parameters, and limit the output to/dev/null. POSIX: defines the functions required by UNIX and UNIX-like systems. 25.-V, -- version indicates that the grep version is incorrect. When you return the grep bugs, the grep version number must be included. 26.-v, -- invert-match: all except Search style rows are displayed. 27.-w, -- word-regexp considers the search style as a word to search. only rows that fully match the word are listed. 28.-x, -- line-regexp considers the search style as a row to be searched. only rows that completely match the "row" are listed.
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.