2018-4-25

9.1 Regular introduction _grep

9.2 grep

9.3 under grep



9.1 Regular introduction _grep

image.png

This chapter is the key point and will be of great help for writing shell scripts in the future.



grep filter keywords

Create a grep directory and copy /etc/passwd to do some experiments.

Basic usage grep 'name' directory

image.png




grep -c number of lines (a few lines are filtered out)

image.png



grep -n line number

image.png


grep -i is case insensitive


grep -v negates and lists the lines without nologin

image.png


grep -r traverse all subdirectories, grep -r 'root' /etc

image.png

These purple ones are all subdirectories.


grep -A, list the following specified lines in addition to the lines of the keyword

image.png

-B -C the same

image.png




9.2 grep

image.png


grep '[0-9]' /etc/inittab //Indicates that filtering out numbers with a range of 0-9 meets the requirements (all lines with numbers


grep -v '^#' /etc/sos.conf //What does ^ mean at the beginning, which means starting with #, that is, listing all files that do not start with #


grep -v '^#' /etc/sos.conf |grep -v '^$' //Filter on the basis of the above


grep -v '^[^a-zA-Z]' test.txt //^ The meaning of the square brackets is the negation of the characters inside


grep 'ro' passwd // This point represents any character, such as r1o rbo roo all meet the conditions


grep ' o*o' passwd // *: The character to the left of the * is repeated 0-n times, 0 times is an o, 1 time is oo


grep '.*' //Any character, all matches


grep 'o\{2\}' /etc/passwd // o appears twice and needs to be untranslated, otherwise {} will not be recognized. is actually o{2} and then untranslated





9.3 under grep

The above grep 'o\{2\}' /etc/passwd needs to be untranslated


No need for de-translation with egrep

egrep 'o{2}' passwd directly recognizes the same as grep 'o\{2\}' /etc/passwd , which is much more convenient.

There is also a grep -E equivalent to egrep


egrep 'o+' /etc/passwd // The character in front of the + sign is one or more times, similar to the * sign, but * has zero times, such as o+t.


egrep 'o? t' /etc/passwd // ? The character before the sign is 0 or 1, if there is ot, if not, it is t


egrep 'root|nologin' /etc/passwd // The vertical line means or, there can be multiple such as a|b|c|d, +i can be case-insensitive.



expand

Put a directory and filter all *.php documents containing eval lines

grep -r --include="*.php" 'eval' /data/






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324850318&siteId=291194637