In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use grep and regular expressions in shell. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. Match the beginning and end characters
Format: grep {^ string | string $} filename
Example: grep ^ [a Merz] tr filename;grep tr$ filename
We see that ^ [amurz] matches characters that begin with the letter amurz, and o $matches characters that end with the letter o.
2. Match the whole word
Format: grep'\ btom\ b' 2.txt
We see the whole word that matches tom.
3 、? The use of + and *
Use: grep {... |? |. *} filename
. Matching a single character can match spaces; * matches the repetition of any character or string multiple times;. + characters must appear once;.? The character appears 0 or 1 times.
Example: a >. The match of b > *. Note: grep does not support +? Metacharacters must have the-E option
We see so much in aa*, which means to match any number of a characters; there are no characters that begin with n when you directly n.+, and you have to add-E to show them; so. + and.? What's the difference? . + is all matched, and.? It just matches the character immediately after the character n.
Let's take a look at the greedy matching of Perl's regular expression-PRV c >. *; d >. *? The lazy match.
We see that the difference between them is that greedy matching matches all to the entire string, while lazy matching just matches the string tom.
4. The label of grep
Format: grep'\ (str\)\ (\)\ (\) [other]\ 1' filename
We see that after matching tom and hello, add two more characters, and then successfully match the first two lines of the string.
5. Pattern search in grep
The format 1:grep'[str]\ {2\} 'filename; searches for lines containing tom twice in the text
The format 2:grep'[str]\ {1,\} 'filename; searches for lines containing tom at least once in the text
The format 3:grep'[str]\ {1J 3} 'filename; search text contains lines containing tom for 1 to 3 times
Lines that begin with tom and appear one or three times in a row
Thank you for reading! This is the end of this article on "how to use grep and regular expressions in shell". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.