In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to use the Grep command in the Linux system, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
The Grep command is mainly used to find the specified string from the file.
First, create a demo_file:
The code is as follows:
$cat demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
This line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
Two lines above this line is empty.
And this is the last line.
Example 01: find the specified string from a single file
The code is as follows:
$grep "this" demo_file
This line is the 1st lower case line in this file.
Two lines above this line is empty.
Example 02: find the specified string from multiple files
The code is as follows:
$cp demo_file demo_file1
$grep "this" demo_*
Demo_file:this line is the 1st lower case line in this file.
Demo_file:Two lines above this line is empty.
Demo_file:And this is the last line.
Demo_file1:this line is the 1st lower case line in this file.
Demo_file1:Two lines above this line is empty.
Demo_file1:And this is the last line.
Example 03: ignore case and use grep-I
The code is as follows:
$grep-I "the" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
This line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
And this is the last line.
Example 04: matching regular expressions in a file
If you can actually use regular expressions, you can greatly improve your efficiency. In the following example, all lines that start with lines and end with empty are matched.
The code is as follows:
$grep "lines.*empty" demo_file
Two lines above this line is empty.
From the perspective of the Grep document, a regular expression must follow the following matching operation.
1.? The preceding item is optional and matched at most once.
2.* The preceding item will be matched zero or more times.
3 + The preceding item will be matched one or more times.
4. {n} The preceding item is matched exactly n times.
5. {n,} The preceding item is matched n or more times.
6. {, m} The preceding item is matched at most m times.
7. {n,m} The preceding item is matched at least n times, but not more than m times.
Example 05: use grep-w to find a full match, excluding substrings
For example, the examples searched with the following examples include "is" and "his"
The code is as follows:
$grep-I "is" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
This line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
Two lines above this line is empty.
And this is the last line.
The result of the search with grep-iw is as follows: note, ignore the size. "IS", "is"
The code is as follows:
$grep-iw "is" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
This line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.
Example 06: check the after/before/around line with grep-A, talk, C,
When performing a grep operation in a large file, if you want to see some of these lines, and want to see before, after, or near certain lines, then the command works. Grep-A, B, B, C, B, B, C, etc. First build a demo.txt as a template
The code is as follows:
$cat demo_text
4. Vim Word Navigation
You may want to do several navigation in relation to the words, such as:
* e-go to the end of the current word.
* E-go to the end of the current WORD.
* b-go to the previous (before) word.
* B-go to the previous (before) WORD.
* w-go to the next word.
* W-go to the next WORD.
WORD-WORD consists of a sequence of non-blank characters, separated with white space.
Word-word consists of a sequence of letters, digits and underscores.
Example to show the difference between WORD and word
* 192.168.1.1-single WORD
* 192.168.1.1-seven words.
6.1 display N lines after matching
The code is as follows:
$grep-A 3-I "example" demo_text
Example to show the difference between WORD and word
* 192.168.1.1-single WORD
* 192.168.1.1-seven words.
6.2 display the first N lines of the match
The code is as follows:
$grep-B 2 "single WORD" demo_text
Example to show the difference between WORD and word
* 192.168.1.1-single WORD
6.3 display the first N lines of the match
The code is as follows:
$grep-C 2 "Example" demo_text
Word-word consists of a sequence of letters, digits and underscores.
Example to show the difference between WORD and word
* 192.168.1.1-single WORD
Example 07: use GREP_OPTIONS to make the items you are looking for stand out
If you want to make the match look good and stand out, you can use the following:
The code is as follows:
$export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'
$grep this demo_file
This line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.
Example 08: use grep-r to search all files and subdirectories
The code is as follows:
$grep-r "ramesh" *
Example 09: use grep-v to display mismatched items
The code is as follows:
$grep-v "go" demo_text
4. Vim Word Navigation
You may want to do several navigation in relation to the words, such as:
WORD-WORD consists of a sequence of non-blank characters, separated with white space.
Word-word consists of a sequence of letters, digits and underscores.
Example to show the difference between WORD and word
* 192.168.1.1-single WORD
* 192.168.1.1-seven words.
Example 10: show all mismatched items
The code is as follows:
$cat test-file.txt
A
B
C
D
$grep-v-e "a"-e "b"-e "c" test-file.txt
D
Example 11: use grep-c to calculate the number of matches
11.1 calculate the number of matching strings
The code is as follows:
$grep-c "go" demo_text
six
11.2 calculate the number of matching patterns
The code is as follows:
$grep-c this demo_file
three
11.3 calculate the number of mismatched patterns
The code is as follows:
$grep-v-c this demo_file
four
Example 12: use grep-l to display matching file names
The code is as follows:
$grep-l this demo_*
Demo_file
Demo_file1
Example 13: only matching strings are displayed
The code is as follows:
$grep-o "is.*line" demo_file
Is line is the 1st lower case line
Is line
Is is the last line
Example 14:
The code is as follows:
$cat temp-file.txt1234512345
$grep-o-b "3" temp-file.txt
2:3
8:3
Note: the above is not the position on the line of the character, but the position of the bytes.
Example 15: display the number of rows with grep-n
The code is as follows:
$grep-n "go" demo_text
5: * e-go to the end of the current word.
6: * E-go to the end of the current WORD.
7: * b-go to the previous (before) word.
8: * B-go to the previous (before) WORD.
9: * w-go to the next word.
10: * W-go to the next WORD.
The above is how to use the Grep command in the Linux system. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.