Regular expressions in CentOS

  • 2020-11-18 06:34:56
  • OfStack

Tools that support linux regular expressions include: grep: find,sed,awk: both are streaming editors that find and replace, and output the replaced text to the screen.

grep tools

grep [-cinvABC] 'word'

-ES13en: Prints the number of lines that meet the requirements

-ES16en: Ignore case

-ES19en: Outputs the desired line and line number.

-ES22en: Prints lines that do not meet the requirements

-ES25en: Followed by a number (with or without Spaces), -A2 means to print the desired line and the following two lines

-ES29en: Followed by the number, -B3 prints the desired line and the three above.

-ES33en: followed by a number, -C24 prints the desired line and four above and four below

# grep -c 'a' 1.txt

# grep -A3 'a' 1.txt

# grep -n 'b' 1.txt filters lines with keywords and displays line Numbers.

# ES49en-ES50en 'ab' 1.txt non-conforming line and line number

# grep [0-9] 1.txt filters out all rows containing Numbers

# ES59en-ES60en [0-9] 1.txt filters out lines that do not contain Numbers

#grep -v '^#' 1.txt filters out all lines that begin with #

# grep-v '^#' 1.txt | grep-v '^$' filters out all lines that start with a blank line and a # ($indicates the end; empty lines can be represented as ^$)

sed tools

sed-n 'a'p filename a is a number indicating the line on which we want to print, and -n means the line on which we want to print. Irrelevant content is not shown.

# ES88en-ES89en '2'p 1.ES91en displays the contents of line 2

# sed-ES95en '1,10'p 1.txt displays lines 1-10

# ES100en-ES101en '1,$'p 1.ES103en displays all rows

# ES106en-ES107en '/aa/ p 1.ES110en prints the line containing aa

Add-e can achieve a variety of behaviors

# ES117en-ES118en '1' ES119en-ES120en '/aabb/' p-ES123en 1.ES124en shows line 1 and the line containing aabb

When querying, exclude certain rows

sed '1' d 1. Exclude the first line when txt displays everything.

sed '1,3'd 1.txt excludes lines 1 through 3

conclusion

Above is the site to you to introduce CentOS regular expressions, I hope to help you, if you have any questions welcome to leave a message, this site will promptly reply you!


Related articles: