In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use the grep command in linux. I hope you will learn a lot after reading this article. Let's discuss it together.
Grep command is a powerful text search tool in Linux system. It can use regular expressions to search for text and print out matching lines.
Linux grep command
The Linux grep command is used to find strings in a file that match the criteria; it can also be used to find files that contain the specified template style. It can use regular expression search to search for a specified string pattern in a file, list file names that contain substrings of matching patterns, and output lines of text that contain that string.
Grep works like this: it searches for string templates in one or more files. If the template includes spaces, it must be referenced, and all strings after the template are treated as file names. The results of the search are sent to standard output without affecting the contents of the original file.
Basic syntax:
Grep [options] pattern [files]
Main parameters of [options]:
-an or-- text: don't ignore binary data.
-An or-- after-context=: displays the content after that line, in addition to the column that matches the template style.
-b or-- byte-offset: marks the number of the first character of the line before displaying the line that matches the style.
-B or-- before-context=: displays the contents before the line, in addition to the line that matches the style.
-c or-- count: calculates the number of columns that match the style.
-C or-- context= or -: displays the content before and after the line, in addition to the line that matches the style.
-d or-- directories=: you must use this parameter when you specify that you are looking for a directory instead of a file, otherwise the grep instruction will return information and stop the action.
-e or-- regexp=: specifies the string as the style for finding the contents of the file.
-E or-- extended-regexp: use the style as an extended normal representation.
-f or-- file=: specifies a rule file whose content contains one or more rule styles and lets grep find the content of the file that meets the rule criteria in the format of one rule style per line.
-F or-- fixed-regexp: a list that treats styles as fixed strings.
-G or-- basic-regexp: use styles as common representations.
-h or-- no-filename: do not indicate the name of the file to which the line belongs until the line that matches the style is displayed.
-H or-- with-filename: indicates the name of the file to which the line belongs before the line that matches the style is displayed.
-I or-- ignore-case: ignore the difference in character case.
-l or-- file-with-matches: lists the file names whose contents match the specified style.
-L or-- files-without-match: lists the file names whose contents do not match the specified style.
-n or-- line-number: marks the column number of the row before displaying the row that matches the style.
-o or-- only-matching: only the matching PATTERN section is displayed.
-Q or-- quiet or-- silent: no information is displayed.
-r or-- recursive: this parameter has the same effect as specifying the "- d recurse" parameter.
-s or-- no-messages: no error message is displayed.
-v or-- revert-match: displays all lines that do not contain matching text.
-V or-- version: displays version information.
-w or-- word-regexp: only columns that match the full word are displayed.
-x-- line-regexp: only the columns that match the entire column are displayed.
-y: this parameter has the same effect as specifying the "- I" parameter.
Display matching lines up and down at the same time? Rows, such as: grep-2 pattern filename displays both the upper and lower lines of the matching line.
The main parameters of the pattern regular expression:
\: ignore 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.
\: to the end of the line matching the regular expression.
[]: single character, such as [A], that is, A meets the requirements.
[-]: range, such as [Amurz], that is, A, B, C all the way up to Z meet the requirements.
.: all single characters.
*: there are characters, and the length can be 0.
Code example:
Example 1: in the current directory, find the file that contains the "test" string in the file prefixed with the word "test" and print out the line of the string. At this point, you can use the following command:
Grep test test*
Output:
Testfile1:This a Linux testfile! # list lines containing test characters in testfile1 files testfile_2:This is a linux testfile! # list lines containing test characters in testfile_2 files testfile_2:Linux test # list lines containing test characters in testfile_2 files
Example 2: reverse lookup, using the "- v" parameter to print out the contents of ineligible lines. Look for lines that do not contain test in files with test in their filename
Grep-v test* test*
Output:
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. After reading this article, I believe you have some understanding of the method of using grep command in linux. If you want to know more about it, welcome to follow the industry information channel. Thank you for your reading!
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.