In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to use grep regular expressions in linux. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
A regular expression is just a description of a string, and string processing can only be done in combination with tools that support regular expressions. This paper takes grep as an example to explain regular expressions.
Grep command
Function: look for strings in each line of the input file.
Basic usage:
Grep [- acinv] [--color=auto] [- A n] [- B n] 'search string' file name
Parameter description:
-a: process binary documents as text
-c: displays the number of matches
-I: ignore case differences
-n: displays the line number at the beginning of the line
-A:After, which displays n lines of data after the matching string
-B:before, which displays the data that matches the first n lines of the string
-v: shows the meaning of no matching line-A:After, shows the meaning of n lines-B:before after the matching part, and shows n lines before the matching part
-- color: highlight matching keywords in a specific color
The-color option is a very good option that allows you to clearly match those characters. It is best to add it to your own .bashrc or .bash _ profile file:
Alias grep=grep-color=auto
After each grep search, the matching effect is automatically highlighted.
The search string is a regular expression. To avoid the influence of shell metacharacters on the regular expression, please enclose it in single quotation marks (''). Never enclose it in double quotation marks ("") or not.
Regular expressions are divided into basic regular expressions and extended regular expressions. Let's briefly summarize each of them.
Basic regular expression
Regular expression learning is mainly the learning of regular expression metadata. There is nothing advanced about regular expressions themselves, so this article just summarizes the metadata of basic regular expressions:
Extended regular expression
Grep generally supports basic regular expressions and supports extended regular expressions through the parameter-E. In addition, grep provides a separate extension command called egrep to support extended regular expressions, which is equivalent to grep-E. In general, though, basic regular expressions are sufficient. In special cases, complex extension expressions can simplify string matching.
Extended regular expressions add some metadata on the basis of basic regular expressions.
The regular expressions below Linux are broad and profound, and the support above summarizes the most commonly used parts. If you are proficient in the regular expressions in the upper part, you can basically meet your daily use.
In addition, many Linux commands support regular expressions, such as find,sed,awk and so on. Please refer to the manual of these commands and use regular expressions when using them.
Linux grep regular expression
Grep regular expression metacharacter set:
The beginning of ^ anchored lines such as:'^ grep' matches all lines that begin with grep.
The end of an anchored line such as: 'grep$' matches all lines ending in grep.
. Matches a character that is not a newline character such as: 'gr.p' matches gr followed by any character followed by p.
* matches zero or more previous characters such as:'* grep' matches all one or more spaces followed by the line of grep. . * used together to represent any character.
[] matches a specified range of characters, such as'[Gg] rep' matches Grep and grep.
[^] matches a character that is not within the specified range, such as:'[^ A-FH-Z] rep' matches the beginning of a letter that does not contain Amurr and Tmurz, followed by a line of rep.
The\ (..\) tag matches characters, such as'\ (love\)', and love is marked as 1.
\ anchor the beginning of a word, such as:'\ match a line that contains a word that begins with grep.
\ > Anchor the end of a word, such as' grep\ > 'matches a line that contains words ending in grep.
The character x\ {m\} is repeated for m times, for example:'0\ {5\} 'matches a line containing 5 os.
X\ {m,\} repeat the character x at least m times, for example:'o\ {5,\} 'matches lines with at least 5 o.
Repeat the character x at least m times and no more than n times, for example:'o\ {5jue 10\} 'matches the lines of 5 Murray 10 o.
\ w matches literal and numeric characters, that is, [A-Za-z0-9], for example:'G\ wimpp 'matches G followed by zero or more literal or numeric characters, followed by p.
\ b word lock, such as:'\ bgrep\ b 'only matches grep.
Common grep options are:
-c outputs only the number of matching rows.
-I is case-insensitive (for single characters only).
-h does not display file names when querying multiple files.
When querying multiple files, only the file names that contain matching characters are output.
-n displays matching lines and line numbers.
-s does not display error messages that do not exist or have no matching text.
-v displays all lines that do not contain matching text.
-V displays software version information
When using grep matching, it is best to use double quotation marks to avoid being mistaken for parameters or special commands by the system, or you can match multiple words.
About the matching instance:
Grep-c "48" test.txt counts how many lines start with the "48" character
Grep-I "May" test.txt looks up all lines of "May" without case sensitivity)
Grep-n "48" test.txt displays the line number; displays the line and line number that match the character "48", which is the same as nl test.txt | grep 48)
Grep-v "48" test.txt display output with no character "48" all lines)
Grep "471" test.txt shows the line where the output character "471" is located)
Grep "48;" test.txt display output begins with the character "48" and is followed by a line with a tab key
Grep "48 [34]" test.txt display output begins with the character "48", and the third character is all lines of "3" or "4")
Grep "^ [^ 48]" test.txt shows that the output line does not begin with the character "48")
Grep "[Mm] ay" test.txt sets case lookup: displays lines that output the first character starting with "M" or "m" and ending with the character "ay")
Grep "K … D" test.txt shows that the first character of the output is "K", the second, third and fourth are arbitrary characters, and the fifth character is the line where "D" is located)
Grep "[Amurz] [9] D" test.txt shows that the output range of the first character is "Amurd", the second character is "9", and the third character is all lines of "D".
Grep "[35].. 1998" test.txt shows that the first character is 3 or 5, the second three characters are arbitrary, and all lines ending in 1998
Grep "4\ {2,\}" test.txt pattern occurrence probability search: displays all lines where the output character "4" is repeated at least twice
Grep "9\ {3,\}" test.txt pattern occurrence probability search: displays all lines where the output character "9" is repeated at least three times
Search for the probability of occurrence of the grep "9" test.txt pattern: shows that the output character "9" is repeated within a certain range, 2 or 3 times for all lines
Grep-n "^ $" test.txt displays the line number of the output blank line
Ls-l | grep "^ d" if you want to query the directory in the directory list, it is the same as: ls-d *
Ls-l | grep "^ d [d]" queries all files in a directory that do not contain a directory
Ls-l | grpe "^ d … .x.x" queries a collection of directories for which other users and members of user groups have executable permissions
After reading the above, do you have any further understanding of how to use grep regular expressions in linux? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.