In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how to use grep and egrep commands in Linux. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Rep / egrep
Syntax: grep [- cinvABC] 'word' filename
-c: print the number of lines that meet the requirements
-I: ignore case
-n: output together with the line number while outputting the lines that meet the requirements
-v: print lines that do not meet the requirements
-A: followed by a number (with or without spaces). For example, A2 means to print the lines that meet the requirements and the following two lines
-B: followed by a number, such as B2, to print the lines that meet the requirements and the two lines above
-C: followed by a number, such as C2, to print lines that meet the requirements and two lines up and down
Print out the line containing 'halt' and the two lines below this line.
[root@localhost ~] # grep-A2 'halt' / etc/passwdhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
Print out the line containing 'halt' and the two lines above this line.
[root@localhost] # grep-B2 'halt' / etc/passwdsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/halt
Print out the line containing 'halt', as well as the two lines above and below this line.
Filter out the line with a keyword and output the line number
[root@localhost ~] # grep-n 'root' / etc/passwd1:root:x:0:0:root:/root:/bin/bash11:operator:x:11:0:operator:/root:/sbin/nologin
Filter lines without a keyword and output the line number
[root@localhost ~] # grep-nv 'nologin' / etc/passwd1:root:x:0:0:root:/root:/bin/bash6:sync:x:5:0:sync:/sbin:/bin/sync7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown8:halt:x:7:0:halt:/sbin:/sbin/halt26:test:x:511:511::/home/test:/bin/bash27:test1:x:512:511::/home/test1:/bin/bash
Filter out all rows that contain numbers
[root@localhost ~] # grep'[0-9]'/ etc/inittab# upstart works, see init (5), init (8), and initctl (8). # 0-halt (Do NOT set initdefault to this) # 1-Single user mode# 2-Multiuser, without NFS (The same as 3, if you do not have networking) # 3-Full multiuser mode# 4-unused# 5-X1Christ 6-reboot (Do NOT set initdefault to this) id:3:initdefault:
Filter out all rows that do not contain numbers
[root@localhost ~] # grep-v'[0-9]'/ etc/inittab# inittab is only used by upstart for the default runlevel.## ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.## System initialization is started by / etc/init/rcS.conf## Individual runlevels are started by / etc/init/rc.conf## Ctrl-Alt-Delete is handled by / etc/init/control-alt-delete.conf## Terminal gettys are handled by / etc/init/tty.conf and / etc/init/serial.conf # with configuration in / etc/sysconfig/init.## For information on how to write upstart event handlers, or how## Default runlevel. The runlevels used are:#
Remove all lines starting with'#'
[root@localhost ~] # grep-v'^ #'/ etc/inittabid:3:initdefault:
Remove all blank lines and lines starting with'#'
[root@localhost ~] # grep-v'^ #'/ etc/crontab | grep-v'^ $'SHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=rootHOME=/
In a regular expression, "^" represents the beginning of the line, "$" represents the end of the line, then the blank line can be represented by "^ $", how to print a line that does not start with the English letter?
[root@localhost ~] # vim test.txt [root@localhost ~] # cat test.txt123abc456abc2323#laksdjfAlllllllll
First write a few lines of strings in test.txt to do experiments.
[root@localhost ~] # grep'^ [^ a-zA-Z] 'test.txt123456#laksdjf [root@localhost ~] # grep' [^ a-zA-Z] 'test.txt123456abc2323#laksdjf
If it's a number, use the form [0-9]. Of course, sometimes you can use the form [15], which contains only 1 or 5, note that it doesn't think it's 15. Write [0-9a-zA-Z] if you want to filter out numbers and uppercase and lowercase letters. In addition, [] has another form, that is, the [^ character] represents a character other than the character in [].
Filter any character and repetition character
[root@localhost ~] # grep'r.. o' / etc/passwdoperator:x:11:0:operator:/root:/sbin/nologingopher:x:13:30:gopher:/var/gopher:/sbin/nologinvcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
. Represents any character. In the above example, the lines that match two arbitrary characters between r and o are filtered out, and * represents zero or more preceding characters.
[root@localhost ~] # grep 'ooo*' / etc/passwdroot:x:0:0:root:/root:/bin/bashlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologin
'ooo*' means oo, ooo, oooo... Or more'o 'now do you have an idea. * what does this combination mean?
[root@localhost ~] # grep'. *'/ etc/passwd | wc-L27 [root@localhost ~] # wc-l / etc/passwd27 / etc/passwd
'. *' indicates zero or more arbitrary characters, and blank lines are included.
Specify the number of times to filter the occurrence of characters
[root@localhost ~] # grep'o\ {2\}'/ etc/passwdroot:x:0:0:root:/root:/bin/bashlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologin
Or you don't need the escape character\ plus-E
Grep-E'o {2}'/ etc/passwd
{} is used here, and its interior is a number, indicating the number of times the preceding character is to be repeated. The above example represents a line that contains two o, namely, 'oo'. Note that we need to add the allusion character'\ 'around {}. In addition, we can also represent a range using {}, which is in the format' {N1 ~ N2}'.
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.