In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "the usage of the Linux basic command grep". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the usage of the basic Linux command grep.
Grep
According to the specified pattern, search for matching lines in the file and display the results in standard output. There are also two instructions, egrep, which are equivalent to grep-Emag and fgrep, which are equivalent to grep-F. If no file name is given, it is read from standard input.
The scope of this command: RedHat, RHEL, Ubuntu, CentOS, SUSE, openSUSE, Fedora.
1. Grammar
Grep [options] PATTERN files
Grep [OPTIONS] [- e PATTERN |-f FILE] [FILE...]
2. List of options
Option
Description
-V |-- version
Display command version information
-- help
Show help documentation
Matching selection
-E |-- extended-regexp
Use extended regular expressions to interpret matching patterns
-F |-- fixed-strings
Treat matching patterns as fixed strings
-G |-- basic-regexp
Use basic regular expressions to interpret matching patterns
-P |-- perl-regexp
Using perl expressions to interpret matching patterns
File and directory selection control
-a |-- text
Ignore binary data
-- binary-files=TYPE
If the first few bytes of a file indicate that the file contains binary data, the file type is assumed to be TYPE. By default, TYPE is binary, and grep usually outputs a line of messages indicating a binary match, and if there is no match, the message is not output. If the types do not match, grep assumes that the binaries do not match; this is equivalent to the-I option. If TYPE is text, grep treats binaries like text; this is equivalent to the-an option. Warning: "grep-binary-files=text" may output binary garbage, which can have serious side effects if the output is a terminal and the terminal driver interprets some of it as commands.
-D action |-- devices=ACTION
If the input file is a device file, it is processed with the specified action. Read, read devices like normal files, skip, ignore devices
-d action |-- directories=ACTION
If you are looking for a catalog file, use the specified action processing. Read, read directories like ordinary files; skip, ignore directories; recursive, recursively read files in subdirectories at that time.
-- exclude=per
Ignore files whose filenames match per
-- exclude-from=FILE
Skip files whose base names globally match any file names read from FILE.
-- exclude-dir=DIR
Excludes directories that match pattern DIR from a recursive search
-I
Handles a binary file as if it did not contain matching data; equivalent to "--binary-files=without-match"
-- include=GLOB
Search only files with basic names that match GLOB
-R |-r |-- recursive
Read all files in the directory recursively
Row number control
-A num |-- after-context=NUM
Displays the matching rows and the number of rows specified later
-B num |-- before-context=NUM
Displays the matching rows and the number of rows specified before
-C num |-NUM |-- context=NUM
Displays matching rows and the specified number of rows before and after
Output line control
-b |-- byte-offset
Print the offset of the first character of the line before the matching line is displayed
-H |-- with-filename
Displays the file name for each matching line, which is the default action if there are multiple files
-h |-- no-filename
Displays the matching lines, but does not display the file name. If you search for a file, this is the default action
-n |-- line-number
Display the line number before displaying the matching lines
-T |-- initial-tab
Make sure that the first character of the actual line content is on the tab stop so that the tab alignment looks normal.
-u |-- unix-byte-offsets
Reports Unix-style byte offsets. This switch causes grep to report byte offsets as if the file were a Unix-style text file
-Z |-- null
Outputs a zero byte (ASCII null character) instead of the character that usually follows the file name. For example, "grep-lz" outputs a zero byte after each file name instead of the usual newline character.
Output control
-c |-- count
Count the number of rows that match
-- color [= WHEN] |-- colour [= WHEN]
Surround matching (non-empty) strings, matching lines, context lines, file names, line numbers, byte offsets, and delimiters (for fields and context line groups) with escape sequences to display their colors on the terminal. Color is defined by the environment variable grep_colors. The deprecated environment variable grep_color is still supported, but its setting has no priority. The valid value for WHEN is never,always,auto.
-L |-- files-without-match
Normal output is disabled; instead, the name of each input file that is not normally output is printed. The scan will stop on the first match.
-l
Displays the file name that matches the specified matching pattern
-m num |-- max-count=NUM
After matching num lines, stop searching
-o |-- only-matching
Show only matching characters for matching lines
-Q |-- quite
Silent mode, returns 0 when the search is complete
-s |-- no-messages
Do not display error messages that the file does not exist and the file is unreadable
Mode control
-e parttern |-- regexp=PATTERN
Set matching mode
-f file |-- file=FILE
Use the contents of the given file as a matching pattern
-I |-- ignore-case
Ignore case
-v |-- invert-match
Display lines that do not match the matching pattern
-w |-- word-regexp
Show lines that match the entire word
-x |-- line-regexp
Show rows that match the entire row
Other options
-- line-buffered
Use line buffering for output. This can lead to performance loss.
-- mmap
If possible, use the mmap (2) system call to read the input instead of the default read (2) system call. In some cases, mmap produces better performance. However,-mmap can cause undefined behavior (including core dumps) if an input file shrinks while grep is running, or if an IUnio error occurs.
-U |-- binary
Treat a file as a binary file.
-z |-- null-data
Think of the input as a set of lines, each ending with a zero byte (ASCII NUL character) instead of a newline character.
3. Environmental variables
The behavior of grep is affected by the following environment variables.
GREP_OPTIONS, which specifies the default option placed before any explicit option. The option specification is separated by spaces. The backslash escapes the next character, so it can specify options that contain spaces or backslashes. For example, if GREP_OPTIONS is'--binary-files=without-match-- directories=skip', then grep executes on the assumption that these two options are already available.
GREP_COLOR, which specifies the color used to highlight matching (non-empty) text.
GREP_COLORS, which specifies the color and other properties used to highlight parts of the output. Its value is a colon-separated list of features, which defaults to ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36, omitting the rv and ne Boolean functions (that is, false). The supported features are shown below.
Sl=, is used for the SGR substring of the entire selected line (that is, matching lines when the-v command line option is omitted, or lines that do not match when-v is specified). However, if the Boolean rv feature and the-v command line option are specified, it is applied to the context matching line. The default value is empty (that is, the default color pair for the terminal).
Cx=, is used for the SGR substring of the entire context line (that is, the unmatched line when the-v command line option is omitted, or the matching line when-v is specified). However, if the Boolean RV feature and the-v command line option are specified, it applies to the selected unmatched lines. The default value is empty (that is, the default color pair for the terminal).
Rv, reverses (swaps) the Boolean values of the "sl=" and "cx=" functions when the-v command line option is specified. The default value is false (that is, functionality is omitted).
Mt=01;31, an sgrg substring used to match non-empty text in any line. (this is only used when the-v command line option is omitted.) When started, the effect of the sl= (or cx=) ability remains active. The default value is the bold red text foreground on the background of the current line.
Ms=01;31, an sgrg substring used to match non-empty text in the selected line. (this is only used when the-v command line option is omitted.) When started, the effect of the sl= (or cx=) ability remains active. The default value is the bold red text foreground on the background of the current line.
Mc=01;31, an sgrg substring used to match non-empty text in the context line. (this is only used when the-v command line option is omitted.) When started, the effect of the sl= (or cx=) ability remains active. The default value is the bold red text foreground on the background of the current line.
Fn=35, the SGR substring for the file name of any content line prefix. The default value is the magenta text foreground on the terminal default background.
Ln=32, the SGR substring of the line number of any content line prefix. The default value is the green text foreground on the terminal's default background.
Bn=32, the SGR substring used for the byte offset of any content line prefix. The default value is the green text foreground on the terminal's default background.
Se=36, when a non-zero context (- -) is specified, the SGR substring is used to insert delimiters between selected row fields (:), between context line fields (-), and between adjacent line groups. The default value is the cyan text foreground on the terminal's default background.
Ne, a Boolean value that prevents erasing inline (EL) to the right (\ 33 [K]) to the end of a line at the end of each color item. This is required on terminals that do not support EL. It is useful in other cases for terminals that do not have the Back_COLOR_ERASE (BCE) Boolean termination feature applied, when the selected highlight color does not affect the background, or when the El is too slow or causes too much flicker. The default value is false (that is, the omission feature)
LC_ALL, LC_COLLATE, LANG, these variables specify the locale for the LC_COLLATE category, which determines the sort sequence used to interpret range expressions, such as [a Murz].
LC_ALL, LC_CTYPE, LANG, these variables specify the locale for the LC_CTYPE category, which determines the type of characters, for example, which characters are spaces.
LC_ALL, LC_MESSAGES, LANG, these variables specify the locale for the LC_MESSAGES category, which determines the language that grep uses for messages. The default C language environment uses American English messages.
POSIXLY_CORRECT, if set, grep behaves as required by POSIX.2; otherwise, grep behaves more like other GNU programs. POSIX.2 requires that the options after the file name must be treated as file names; by default, these options are ranked before the list of operands and are treated as options. In addition, POSIX.2 requires that unrecognized options be diagnosed as "illegal", but since they do not really violate the law, the default is to diagnose them as "invalid". POSIXLY_RIDER also disables _ NationGNUNOOPTION ARGVLANGSION, as described below.
_ NumberGNU nonoptionality, where N is the numeric process ID of grep. If the ith character of this environment variable value is 1, do not treat the ith Operand of grep as an option, even if it seems to be one of the options. Shell can place this variable in the environment for each command it runs, specifying which operands are the result of filename wildcard expansion and should not be considered an option. This behavior is available only in the GNU C library, and only if POSIXLY_RIDER is not set.
4. Exit code
Typically, if the selected row is found, the exit status is 0, otherwise it is 1. However, if an error occurs, the exit status is 2 unless you use the-Q,-- quite,-- slient options and locate the selected row. Note, however, that for programs such as grep, CMP, and diff, POSIX only requires that the exit status be greater than 1 in the event of an error; therefore, for portability reasons, it is recommended that you use logic that tests this general condition rather than logic that is strictly equal to 2.
5. Examples
1) display the lines in the file that begin with 2
[root@localhost weijie] # cat 2.c
one hundred and twenty three
twenty-three
two hundred and twelve
[root@localhost weijie] # grep ^ 2 2.c / / display lines starting with 2
twenty-three
two hundred and twelve
2) display lines that do not contain 23
[root@localhost weijie] # grep-v 23 2.c
two hundred and twelve
3) display lines that match the entire word
[root@localhost weijie] # grep-w 23 2.c
twenty-three
At this point, I believe you have a deeper understanding of "the use of the Linux basic command grep". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.