[root@shell ~]# grep "^[hH]" /etc/rc.d/rc.rc.sysinit
[root@shell ~]# grep "sh$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
kxy:x:1000:1000:kxy:/home/kxy:/bin/bash
fox:x:1001:1001::/home/fox:/bin/bash
[root@shell ~]# egrep "^#[[:space:]]+[^[:space:]]*" /etc/fstab
# /etc/fstab
# Created by anaconda on Sun Sep 17 13:46:24 2023
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
[root@shell ~]# egrep -w -o "(to)[a-z]+\1" /etc/rc.d/rc.local
[root@shell ~]# grep -w -o "s[a-z]*d" /etc/inittab?
systemd
systemd
[root@shell ~]# ifconfig | egrep -o -w "[1-9]{1}|([1-9]{1}[0-9]{1})|[1-2]{1}([0-9]{1,2})" | awk '{printf "%s ",$0} END {print ""}'
192 168 110 132 255 255 255 192 168 110 255 64 29 78 73 5 3 73 127 1 255 1 128 17 1 9 17 1 9
[root@shell ~]# egrep "(Failed|FAILED)" /var/log/secure
[root@shell ~]# grep -i "Failed" /var/log/secure
[root@shell ~]# grep "bash$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
kxy:x:1000:1000:kxy:/home/kxy:/bin/bash
fox:x:1001:1001::/home/fox:/bin/bash
[root@shell ~]# ll /etc/ | grep '^.*/ns.*\.conf$'
lrwxrwxrwx. ?1 root root ? ? ? ?29 Sep 17 21:55 nsswitch.conf -> /etc/authselect/nsswitch.conf
[root@shell ~]# ll /etc/ns*.conf
lrwxrwxrwx. 1 root root 29 Sep 17 21:55 /etc/nsswitch.conf -> /etc/authselect/nsswitch.conf
[root@shell ~]# grep -o ".:." /etc/passwd
[root@shell ~]# egrep -o -w "([a-z])[a-z]+\1" /etc/services
[root@shell ~]# cat file.txt
48? ? ? ? Dec? ? ? 3BC1977 LPSX ? 68.00 ? LVX2A? ? 138
483? ? ? Sept? ? ?5AP1996 USP? ? ?65.00 ? LVX2C? ?189
47? ? ? ? Oct? ? ? 3ZL1998 LPSX? ? 43.00 ? KVM9D ? 512
219? ? ? dec? ? ? 2CC1999 CAD ? ?23.00 ? PLV2C? ? ?68
484? ? ? nov? ? ? 7PL1996 CAD? ? ?49.00 ? PLV2C? ? ?234
483? ? ? may ? ? 5PA1998 USP? ? ?37.00 ? KVM9D? ? 644
216? ? ? sept? ? ?3ZL1998 USP? ? ?86.00 ? KVM9E? ? 234
[root@shell ~]# grep "48" file.txt
[root@shell ~]# grep -n "48" file.txt?
[root@shell ~]# grep -w "48" file.txt?
[root@shell ~]# grep "^48[34]" file.txt
[root@shell ~]# egrep "^48(3|4)" file.txt
[root@shell ~]# grep "^[^48]" file.txt
[root@shell ~]# grep -i "Sept" file.txt
[root@shell ~]# grep ?"K.*D" file.txt
[root@shell ~]# egrep ?"[A-Z]{2}..C" file.txt
[root@shell ~]# egrep ?"5.*199(6|8)" file.txt
[root@shell ~]# grep 'bash$' /etc/passwd
root:x:0:0:root:/root:/bin/bash
kxy:x:1000:1000:kxy:/home/kxy:/bin/bash
fox:x:1001:1001::/home/fox:/bin/bash
[root@shell ~]# egrep -o "[[:digit:]]{3,4}" /etc/passwd
100
6553
6553......
1001
977
977这里太多了,可以使用awk再做处理,用printf格式化输出
[root@shell ~]# egrep -o "[[:digit:]]{3,4}" /etc/passwd | awk '{ printf "%s ", $0 } END { print "" }'
100 6553 6553 999 997 998 996 997 993 996 992 172 172 995 991 994 990 993 989 992 988 991 986 990 985 989 984 988 983 987 982 986 981 985 980 978 978 1000 1000 1001 1001 977 977?这里解释一下吧!
printf "%s "注意%s后有个空格,$0为awk的内置变量,表示当前行的全部内容,这里使用格式化输出printf取消换行符并用空格隔开,END为awk的动作,这里为再结尾打印一个空行,实质上就是一个换行符
[root@shell ~]# egrep "^[[:space:]]+[^[:space:]]*" /etc/grub2.cfg?
? load_env -f ${config_directory}/grubenv
? load_env
? ?set default="${next_entry}"
? ?set next_entry=
? ?save_env next_entry
? ?set boot_once=true
? ?set default="${saved_entry}"......
?source ${config_directory}/custom.cfg
? source $prefix/custom.cfg
[root@shell ~]# netstat -tan | egrep "LISTEN[[:space:]]*$"
tcp ? ? ? ?0 ? ? ?0 0.0.0.0:22 ? ? ? ? ? ? ?0.0.0.0:* ? ? ? ? ? ? ? LISTEN ? ??
tcp ? ? ? ?0 ? ? ?0 127.0.0.1:631 ? ? ? ?0.0.0.0:* ? ? ? ? ? ? ? LISTEN ? ??
tcp6 ? ? ?0 ? ? ?0 :::22? ? ? ? ? ? ? ? ? ? ? ?:::*? ? ? ? ? ? ? ? ? ? ? ? LISTEN ? ??
tcp6 ? ? ?0 ? ? ?0 ::1:631? ? ? ? ? ? ? ? ? ?:::*? ? ? ? ? ? ? ? ? ? ? ? LISTEN?
[root@shell ~]# fdisk -l | egrep "/dev/(sd|hd)[[:alpha:]]"? ?#这里没有结果
[root@shell ~]# ldd /usr/bin/cat
? ? ? ? linux-vdso.so.1 (0x00007fff8df01000)
? ? ? ? libc.so.6 => /lib64/libc.so.6 (0x00007f3f05dec000)
? ? ? ? /lib64/ld-linux-x86-64.so.2 (0x00007f3f0600b000)[root@shell ~]# ldd /usr/bin/cat | grep -o "/[^ ]*"
/lib64/libc.so.6
/lib64/ld-linux-x86-64.so.2
[root@shell ~]# egrep "^(S|s)" /proc/meminfo
[root@shell ~]# grep -i "^s" /proc/meminfo
[root@shell ~]# egrep ?"^[sS]" /proc/meminfo
[root@shell ~]# egrep "^(root|apache|nginx)" /etc/passwd
root:x:0:0:root:/root:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
nginx:x:977:977:Nginx web server:/var/lib/nginx:/sbin/nologin
[root@shell ~]# echo "/var/www/html/index.html" | egrep -o [^/]+$? ? ?# [^/]表示不包含/
index.html
[root@shell ~]# ifconfig | egrep -o -w "[1-9]{1}|([1-9]{1}[0-9]{1})|[1-2]{1}([0-9]{1,2})" | awk '{printf "%s ",$0} END {print ""}'
192 168 110 132 255 255 255 192 168 110 255 64 29 78 73 5 3 73 127 1 255 1 128 17 1 9 17 1 9
[root@shell ~]# egrep "^(.+):.*\1$" /etc/passwd | cut -d: -f1
sync
shutdown
halt[root@shell ~]# egrep "^(sync|shutdown|halt)" /etc/passwd? ? ? ?#检测一下是否正确
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt