Example Analysis of regular expressions in CentOS
This article mainly introduces the example analysis of regular expressions in CentOS, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.
The tools that support linux regular expressions are: grep: implement search, sed,awk: are streaming editors that can find and replace, and output the replaced text to the screen.
Grep tool
Grep [- cinvABC] 'word'
-c: print the number of lines that meet the requirements
-I: ignore case
-n: output lines and line numbers that meet the requirements.
-v: print lines that do not meet the requirements
-A: followed by a number (with or without spaces),-A2 means to print the lines that meet the requirements and the following two lines
-B: followed by a number,-B3 prints the lines that meet the requirements and the three lines above.
-C: followed by a number,-C24 prints lines that meet the requirements and four lines up and down
# grep-c 'a'1.txt
# grep-A3 'a'1.txt
# grep-n 'b' 1.txt filters rows with keywords to display line numbers.
# grep-nv 'ab' 1.txt lines and line numbers that do not meet the requirements
# grep [0-9] 1.txt filters out all rows that contain numbers
# grep-v [0-9] 1.txt filters out rows that do not contain numbers
# grep-v'^ # '1.txt filters out all lines that begin with #
# grep-v'^ # '1.txt | grep-v' ^ $'filter out all blank lines and lines starting with # ($means ending, blank lines can be represented by ^ $)
Sed tool
Sed-n 'a'p filename an is a number that indicates the number of lines, and-n represents the lines we want to print. Unimportant content is not displayed.
# sed-n '2roomp 1.txt displays the second line
# sed-n '1re10 1.txt displays 1-10 lines of content
# sed-n'1 recording 1.txt displays all lines
# sed-n'/ aa/'p 1.txt prints lines containing aa
Add-e to achieve a variety of behaviors
# sed-e '1roomp-e' / aabb/'p-n 1.txt displays the first line and the line containing aabb
When querying, exclude certain rows
When sed'1' d 1.txt displays everything, the first line is excluded.
Sed '1jin3 1.txt excludes lines 1 to 3
Thank you for reading this article carefully. I hope the article "sample Analysis of regular expressions in CentOS" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!