Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the regular expression grep correctly

2025-04-02 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 correctly use the regular expression grep, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

Syntax: grep option 'word' filename

-c: print the number of lines that meet the requirements

-I: case-insensitive

-n: displays the line number while outputting lines that meet the requirements

-v: print lines that do not meet the requirements

-A: followed by a number n, indicating that the lines that meet the requirements are printed and the following n lines

-B: followed by a number n, indicating that the lines that meet the requirements and the n lines above are printed

-C: followed by a number n, indicating that the lines that meet the requirements and the upper and lower n lines are printed

-r: all the files in the directory will be traversed

-- color: Mark the matching keywords in red

Example:

Filter out the line with a keyword and output the line number

Grep-n 'root' 1.txt

Filter out lines without a keyword and output line numbers

Grep-nv 'root' 1.txt

Filter out all rows that contain numbers

Grep'[0-9] '1.txt

Filter out all rows that do not contain numbers

Grep-v'[0-9] '1.txt

Remove all lines that begin with #

Grep-v'^ # '1.txt

Remove all blank lines and lines that begin with #

Grep-v'^ $'1.txt | grep-v' ^ #'

Filter out lines that begin with the English letter

Grep'^ [a-zA-Z] '1.txt

Filter out lines that start with a non-number

Grep-v'^ [0-9] '1.txt or grep' ^ [^ 0-9] '1.txt

Filter any one or more characters

Grep 'r.o' 1.txtten GREP' rt' 1.txtten GREP 'r.t' 1.txt

Filter out the row containing root and the following line

Grep-A1 'root' 1.txt

Filter out the row containing root and the above line

Grep-B1 'root' 1.txt

Specify the number of filtered characters

Grep'o {2} '1.txt

Filter all lines containing eval in .php documents under a directory

Grep-r-include= ".php" 'eval' / data/

Filter two or more keywords

Grep-E '123 | abc' filename # find the lines in the file that contain 123 or abc

Or egrep '123 | abc' filename

Or awk'/ 123 | abc/' filename

Egrep=grep-E

Match one or more + characters

Egrep 'oasis' 1.txt

Match 0 or 1? The preceding character

Egrep 'oasis' 1.txt

Match roo or match body

Egrep 'roo | body' 1.txt

Using parentheses to represent a whole, the following example matches roo or ato

Egrep'r (oo) | (at) o' 1.txt

Match one or more oo

Egrep'(oo) + '1.txt

.: represents any character

: represents 0 or more preceding characters

+: represents one or more characters preceding the +

?: means 0 or 1? The preceding character

. *: represents any arbitrary character (including blank lines)

Note: + and? Grep does not support it, but egrep only supports it.

The above is how to use the regular expression grep correctly. Have you learned the 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: 208

*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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report