In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to use regular expressions in Linux to search for text in files. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
A regular expression is a symbolic representation used to identify text patterns. Linux's main program for dealing with regular expressions is grep. Grep searches for rows that match the regular expression and feeds the results to standard output.
1. Grep matching pattern
Grep accepts options and parameters as follows (where regex represents a regular expression)
The copy code is as follows:
Grep [options] regex [files]
Options is mainly the following table:
Option meaning function description-iignore case ignores case-vinvert match mismatches-lfile-with-match output matched file names-Lfile-without-match output mismatched file names-number of ccount output matches (lines)-nnumber outputs matching lines while adding the file name and the number of lines in the file name-hno-filename suppresses the output of the file name
As a Linux administrator, you need to process text files. You can use different tools such as grep, awk, and sed to find files that contain specific text strings. Here, I'll introduce a way to use regular expressions to search for text in a file in a flexible way.
Let's consider an example where regular expressions work. For example, when you try to use the command grep-r host /, the result will be huge. Because every word that contains the text string host (a word like ghostscript) will match. With regular expressions, you can better customize what you are looking for. For example, you can use the regular expression'^ host', to tell grep to find only lines that start with host.
Regular expressions are not available in all commands, and the commands you use must be programmed so that regular expressions can be used. The most commonly used of these commands are grep, tr, and vi. Other tools, such as sed and awk, can also use regular expressions.
An example of using regular expressions is:
The copy code is as follows:
Grep 'lin.x' *
The points in the regular expression 'lin.x' have a special meaning. It matches any character in that position. To prevent interpretative problems, I recommend that you always put regular expressions between single quotation marks, which prevents shell from interpreting regular expressions.
Use regular expressions
You can do a lot of things with regular expressions. In the following list, you can find some examples of the most common and commonly used regular expressions.
* ^: indicates that the text string must be at the beginning of a line. So, when looking for lines that only start with "hosts", you can use the command: grep-ls'^ hosts'
* $: represents the end of a line. So, when looking for lines that only end with "hosts", you can use the command: grep-ls' hosts$'
* you can combine ^ and $in a regular expression to find lines that contain only "yes", using the command grep-ls'^ yes$'
*.: a wildcard that can refer to any character except a newline character. To find a line that contains tex, tux, tox, or tix, you can use: grep-ls 't.x'
* []: indicates that the characters between square brackets are optional in a regular expression. To find a user with the name pinda or linda, use the command: grep-ls'[pl] inda'
* [^]: ignore all characters after ^ in square brackets. To find lines that contain the text linda and ignore lines that contain only linda or pinda, the command is: grep-ls'[^ pl] inda'
* -: represents a class or range of characters. This is very useful in commands like tr. The following command converts all lowercase letters to uppercase letters: tr Amurz Amurz
< mytext。同样地,你可以使用正则表达式grep -ls '^0-9'去查找其中有一些行是以数字开头的文件。 * \< 和 \>Find the pattern at the beginning or end of a word The command to find the word at the beginning of a line that starts with "san" is: grep\. These regular expressions have two disadvantages-they do not find lines that start with the specified regular expression, and they are not supported by all tools. However, vi and grep support this usage.
*\: make sure that characters with special meanings in regular expressions are not interpreted. The command to find a text string that starts with any character, followed by the text "host" is grep-ls' .host'. And if you need to find the text string with the dot as the first character, followed by "host", you can use the command: grep-ls'\ .host'
These regular expressions can help you find words that contain specific strings of text. You can also use regular expressions to specify how often the string appears in a word. For example, you can use regular expressions to search for files that contain the user name "linda" exactly three times. To do this, you need to use the repetition operator of the regular expression and ensure that the entire regular expression is in quotation marks. Without quotation marks, you may cause shell to interpret your repetition operator.
The list of the most important repeat operators is as follows:
* *: indicates that the above-mentioned regular expression may appear once, multiple times, or not at all. Note: don't be confused with * in shell-in shell, * represents any character, while in regular expressions, * indicates that previous regular expressions may exist.
*?: indicates that it may be a character at that position (but it doesn't have to be). For example, the command to find both color and colour is: grep-ls' colo.r'
* +: indicates that the previous character or regular expression must appear at least once
*\ n\}: indicates that the previous character or regular expression must appear at least n times. This is useful when looking for a number between 100and 999: grep-ls'0-9\ {3\}'
The above is how to use regular expressions to search for text in files in Linux. Have you learned any 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.