Linux grep command details how to use Linux grep command

Source: Internet
Author: User

The grep command is used to find files whose content contains the specified template style. If the content of a file conforms to the specified template style, the default grep command displays the column containing the template style. If no file name is specified or the given file name is "-", The grep command reads data from the standard input device.

Syntax

Grep [-abcEFGhHilLnqrsvVwxy] [-A <display columns>] [-B <display columns>] [-C <display columns>] [-d <perform action>]

[-E <Template style>] [-f <Template File>] [-- help] [template style] [file or directory...]

Parameters:

  • -A or -- text do not ignore binary data.
  • -A <show the number of columns> or -- after-context = <show the number of columns> not only displays the column that conforms to the template style, but also displays the content after the column.
  • -B or -- byte-offset indicates the first character of the column before displaying the column that conforms to the template style.
  • -B <display number of columns> or -- before-context = <display number of columns> In addition to displaying the column that conforms to the template style, and displaying the content before the column.
  • -C or -- count is used to calculate the number of columns that conform to the template style.
  • -C <display columns> or -- context = <display columns> or-<display columns> displays the content before and after a column that conforms to the template style.
  • -D <perform action> or -- directories = <perform action> this parameter is required when you specify a directory rather than a file to be searched, otherwise, the grep command returns the information and stops the action.
  • -E <Template style> or -- regexp = <Template style> specifies the string as the template style for searching the file content.
  • -E or -- extended-regexp use the template style as an extended normal notation.
  • -F <Template file> or -- file = <Template file> specifies the template file, whose content contains one or more template styles, so that grep can find the content of the file that meets the template conditions, the format is a template style for each column.
  • -F or -- fixed-regexp treats the template style as a list of fixed strings.
  • -G or -- basic-regexp treats the template style as a normal notation.
  • -H or -- no-filename does not indicate the file name of the column before displaying the column that conforms to the template style.
  • -H or -- with-filename indicates the file name of the column before displaying the column that conforms to the template style.
  • -I or -- ignore-case ignore the differences in case of characters.
  • -L or -- file-with-matches: lists the names of files whose content conforms to the specified template style.
  • -L or -- files-without-match lists the names of files whose contents do not conform to the specified template style.
  • -N or -- line-number indicates the number of columns in the column before displaying the column that conforms to the template style.
  • -Q or -- quiet or -- silent does not display any information.
  • -R or -- recursive this parameter has the same effect as the specified "-d recurse" parameter.
  • -S or -- no-messages do not display error messages.
  • -V or -- revert-match reverse lookup.
  • -V or -- version displays version information.
  • -W or -- word-regexp only displays columns that match the full word.
  • -X or -- line-regexp only displays columns that match the full column.
  • -Y: The effect of this parameter is the same as that of the specified "-I" parameter.
  • -- Help online help.

Instance

1. In the current directory, find the file with the suffix "test" containing the "test" string and print the line of the string. In this case, run the following command:

Grep test * file

The result is as follows:

$ Grep test * # search for files with the suffix "test" containing the "test" string

Testfile1: This a Linux testfile! # List the lines containing the test characters in the testfile1 file

Testfile_2: This is a linux testfile! # List the lines containing the test characters in the testfile_2 file

Testfile_2: Linux test # list the lines containing the test characters in the testfile_2 file

2. Search for eligible files recursively. For example, search for all files in the/etc/acpi directory and its subdirectories (if any) that contain the "update" string and print the content of the row where the string is located, the command used is:

Grep-r update/etc/acpi

The output result is as follows:

$ Grep-r update/etc/acpi # Search for "etc/acpi" recursively"

# File containing "update"

/Etc/acpi/ac. d/85-anacron.sh: # (Things like the slocate updatedb cause a lot of IO.) Rather

/Etc/acpi/resume. d/85-anacron.sh: # (Things like the slocate updatedb cause a lot of IO.) Rather

/Etc/acpi/events/thinkpad-cmos: action =/usr/sbin/thinkpad-keys -- update

3. Reverse search. The preceding examples show how to find and print the rows that meet the condition. The "-v" parameter can be used to print the contents of the rows that do not meet the condition.

Find the row whose file name contains test but does not contain test. The command is as follows:

Grep-v test *

The result is as follows:

$ Grep-v test * # Find the row in the file with the file name containing test that does not contain test

Testfile1: helLinux!

Testfile1: Linis a free Unix-type operating system.

Testfile1: Lin

Testfile_1: hello linux!

Testfile_1: linux is a free UNIX-TYPE opterating system.

Testfile_1: this is a linux testfile!

Testfile_2: hello linux!

Testfile_2: Linux is a free unix-type opterating system.

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.