Grep for Linux Three Musketeers
The three most important Linux commands are known in the industry as the Three musketeers: awk, sed, and grep. Sed has been covered in the previous article **, this article is about grep command.
In Linux system, grep command is frequently used. Mastering the common usage of grep can greatly improve your work efficiency.
The grep command is a powerful text search tool. It can use regular expressions to match the specified pattern and print the matched lines. Note that grep only supports matching and does not replace matched content. Sed can do this.
In general, grep is relatively simple. Instead of listing all options and parameters in detail, this article uses several specific examples to illustrate the use methods and scenarios of grep to help you quickly learn common uses of grep.
Examples of actual combat
Let’s cut the crap and get right to it. The example in this article requires a sample file that looks like this:
1. Filter out the lines that contain syslog
2. Filter out lines that start with NTP
3. Filter out the lines matching NTP and the following two lines
4. Filter out the line containing syslog and the upper line
5. Filter out the contents including syslog and the upper and lower lines
6. Filter a keyword and output the line number
7. Filter the keyword and output the line number
8. Delete blank lines
9. Filter the rows containing root or syslog
10. View all files in the current directory that contain a keyword (this is useful)
Simple summary
Through some simple cases, we should be familiar with the common use of grep. Here is a brief summary of the common options of grep. I believe that the summary after actual practice will have a better learning effect.
-A
: Displays the line in addition to the matching lineafterN line-B
: Displays the line in addition to the matching linebeforeN line-C
: Displays the line in addition to the matching lineBefore and afterN line-c
: Statistics the number of matched rows-e
:Implement logical OR relationships between multiple options-E
:Support for extended regular expressions-F
: is equivalent to fgrep-i
:Ignore case-n
: Displays the matching line number-o
: Displays only the matched string-q
: The quiet mode is used in scripts and no information is output-s
: No error information is displayed-v
:Displays unmatched rows-w
: Display the entireThe word--color
: Highlights the matched string in color
Egrep and fgrep are similar to grep, but not very practical. Their functions can be realized by the extended parameters of grep, so they will not be extended.
That’s all. Thank you for reading. I’m Chopin. Pay attention to my public number “programming accomplishment”, a large number of dry goods articles waiting for you!
Public number background reply “1024” have surprise!
Recommended reading:
- A letter for Linux beginners
- The most detailed load balancing principle diagram in the whole network
- Sed tutorial details, xiao Bai can understand
- Linux three Musketeers grep tutorial details
- Linux file search artifact find actual combat detailed solution, recommended collection!