In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you about how to use the grep command in Linux. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
1. Brief introduction to grep
Grep (global search regular _ expression (RE) and print out the line, full search for regular expressions and printing lines) is a powerful text search tool that uses regular expressions to search for text and print matching lines. Unix's grep family includes grep, egrep and fgrep. The commands of egrep and fgrep are only slightly different from grep. Egrep is an extension of grep to support more re metacharacters. Fgrep is fixed grep or fast grep, which treats all letters as words, that is, metacharacters in regular expressions express their own literal meaning and are no longer special. Linux uses the GNU version of grep. It is more powerful, and you can use the functions of egrep and fgrep through the-G,-E,-F command line options.
Grep works like this by searching 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 the screen without affecting the contents of the original file.
2. POSIX character class
In order to maintain consistency in character encodings in different countries, POSIX (The Portable Operating System Interface) adds special character classes, such as [: alnum:] is another word for A-Za-z0-9. You need to put them in the [] sign to become regular expressions, such as [A-Za-z0-9] or [: alnum:]. Grep under Linux supports POSIX character classes except fgrep.
[: alnum:]
Alphanumeric character
[: alpha:]
Text character
[: digit:]
Numeric character
[: graph:]
Non-empty characters (non-spaces, control characters)
[: lower:]
Lowercase character
[: cntrl:]
Control character
[: print:]
Non-empty characters (including spaces)
[: punct:]
punctuation
[: space:]
All white space characters (new lines, spaces, tabs)
[: upper:]
Uppercase character
[: xdigit:]
Hexadecimal digits (0-9 mae mai mai f mai F)
3.Grep command options
-?
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.
-b _ m _ m _ r _ b _ b
Print the block number in front of the print match line.
-cmam Mutual account
Only the number of matching lines is printed, and no matching content is displayed.
-f File,--file=File
Extract the template from the file. The empty file contains 0 templates, so nothing matches.
-hmam, Mustang, Mustang, no, filename.
When searching for multiple files, the matching file name prefix is not displayed.
-imam, music, music, recase.
Ignore case differences.
-qmam, music, music
Cancel the display and return only to the exit status. 0 indicates that a matching row has been found.
-lmmolation, talk, talk.
Print a list of files that match the template.
-Lmam talk, talk, files, talk, match.
Print a list of files that do not match the template.
-nmam Mutual talk lineline ripple number
Print the line number in front of the matching line.
-Scrimson, Murphy.
No error message is displayed about the file that does not exist or cannot be read.
-vmam, talk, talk, match.
Inverse retrieval, showing only rows that do not match.
-wmil talk, regexp
If referenced, use the expression as a word search.
-VMagneWhile version
Displays software version information.
4. Example
To make good use of grep this tool, in fact, is to write a good regular expression, so here do not explain all the functions of grep examples, only a few examples to explain the writing of a regular expression.
$ls-l | grep'^ a'
Filter the ls-l output through the pipeline, showing only lines that start with a.
$grep 'test' d*
Displays lines that contain test in all files that begin with d.
$grep 'test' aa bb cc
Displays the lines that match the test in the aa,bb,cc file.
$grep'[a Merz]'aa
Displays all lines that contain at least five consecutive lowercase characters for each string.
$grep'w (es) t.collect'aa
If the west is matched, the es is stored in memory and marked as 1, and then any character (. *) is searched, followed by another es (), and the line is displayed when found. If you use egrep or grep-E, instead of using the "" sign for escape, you can just write'w (es) t. Escape'.
The above is how to use the grep command in Linux. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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: 204
*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.