grep&egrep命令
grep命令
-c 行數 -i 不區分大小寫 -n 顯示行號 -v 取反 -r 遍歷所有子目錄 -A 後面跟數字,過濾出符合要求的行以及下面n行 -B 同上,過濾出符合要求的行以及上面n行 -C 同上,同事過濾出符合要求的行以及上下各n行
例項
[[email protected] grep]# pwd /root/grep [[email protected] grep]# touch passwd [[email protected] grep]#tail -n 20 /etc/passwd >> passwd```擷取/etc/passwd前20行追加到/root/grep/ passwd中 [
[email protected] grep]# grep 'nologin' passwd 篩選‘nologin’字元。 qemu:x:107:107:qemu user:/:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin radvd:x:75:75:radvd user:/:/sbin/nologin chrony:x:994:990::/var/lib/chrony:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin geoclue:x:993:988:User for geoclue:/var/lib/geoclue:/sbin/nologin sssd:x:992:987:User for sssd:/:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin gnome-initial-setup:x:991:986::/run/gnome-initial-setup/:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin gluster:x:990:984:GlusterFS daemons:/var/run/gluster:/sbin/nologin unbound:x:989:983:Unbound DNS resolver:/etc/unbound:/sbin/nologin
-c 檢視passwd中包含‘nologin’的行數
[[email protected] grep]# grep -c 'nologin' passwd
18
-n 檢視passwd中包含‘nologin’所在的行號
[[email protected] grep]# grep -n 'nologin' passwd 1:qemu:x:107:107:qemu user:/:/sbin/nologin 2:ntp:x:38:38::/etc/ntp:/sbin/nologin 3:radvd:x:75:75:radvd user:/:/sbin/nologin 4:chrony:x:994:990::/var/lib/chrony:/sbin/nologin 5:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin 6:usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin 7:geoclue:x:993:988:User for geoclue:/var/lib/geoclue:/sbin/nologin 8:sssd:x:992:987:User for sssd:/:/sbin/nologin 9:gdm:x:42:42::/var/lib/gdm:/sbin/nologin 10:rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin 11:nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin 12:gnome-initial-setup:x:991:986::/run/gnome-initial-setup/:/sbin/nologin 13:avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin 14:postfix:x:89:89::/var/spool/postfix:/sbin/nologin 15:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 16:tcpdump:x:72:72::/:/sbin/nologin 19:gluster:x:990:984:GlusterFS daemons:/var/run/gluster:/sbin/nologin 20:unbound:x:989:983:Unbound DNS resolver:/etc/unbound:/sbin/nologin
-i 檢視passwd中包含‘nologin’的字元並且不區分大小寫
手動修改passwd中的檔案某一行nologin中no為大寫
qemu:x:107:107:qemu user:/:/sbin/NOlogin
ntp:x:38:38::/etc/ntp:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
[[email protected] grep]# grep -ni 'nologin' passwd
1:qemu:x:107:107:qemu user:/:/sbin/NOlogin 查詢到NO為大寫的一行
2:ntp:x:38:38::/etc/ntp:/sbin/nologin
3:radvd:x:75:75:radvd user:/:/sbin/nologin
4:chrony:x:994:990::/var/lib/chrony:/sbin/nologin
5:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
6:usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
7:geoclue:x:993:988:User for geoclue:/var/lib/geoclue:/sbin/nologin
8:sssd:x:992:987:User for sssd:/:/sbin/nologin
9:gdm:x:42:42::/var/lib/gdm:/sbin/nologin
10:rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
11:nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
12:gnome-initial-setup:x:991:986::/run/gnome-initial-setup/:/sbin/nologin
13:avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
14:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
15:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
16:tcpdump:x:72:72::/:/sbin/nologin
19:gluster:x:990:984:GlusterFS daemons:/var/run/gluster:/sbin/nologin
20:unbound:x:989:983:Unbound DNS resolver:/etc/unbound:/sbin/nologin
-v 檢視passwd中除了‘nologin’以外的行
[[email protected] grep]# grep -niv 'nologin' passwd
17:user:x:1000:1000:user:/home/user:/bin/bash
18:admin:x:1001:1001::/home/admin:/bin/bash
-r 檢視etc下包含了root字元的所有目錄及檔案
[[email protected] grep]# grep -r 'root' /etc/
/etc/fstab:/dev/mapper/centos-root / xfs defaults 0 0
/etc/pki/ca-trust/ca-legacy.conf:# The upstream Mozilla.org project tests all changes to the root CA
/etc/pki/ca-trust/ca-legacy.conf:# to temporarily keep certain (legacy) root CA certificates trusted,
/etc/pki/ca-trust/ca-legacy.conf:# It may keep root CA certificate as trusted, which the upstream
/etc/pki/ca-trust/extracted/README:root CA certificates.
/etc/pki/ca-trust/extracted/java/README:root CA certificates.
匹配到二進位制檔案 /etc/pki/ca-trust/extracted/java/cacerts
/etc/pki/ca-trust/extracted/openssl/README:root CA certificates.
/etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt:# Comodo AAA Services root
/etc/pki/ca-trust/extracted/pem/README:root CA certificates.
/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem:# Comodo AAA Services root
/etc/pki/ca-trust/extracted/pem/email-ca-bundle.pem:# Comodo AAA Services root
/etc/pki/tls/certs/make-dummy-cert: echo [email protected]lhost.localdomain
/etc/pki/tls/openssl.cnf:dir = ./demoCA # TSA root directory
/etc/rpm/macros.perl-srpm:# from git source repository. This file must present in minimal build root to
/etc/rpm/macros.perl-srpm:# This file must not use Perl or any non-minimal-build-root package. The
/etc/rpm/macros.perl:%define perl_br_testdir %{buildroot}%{perl_testdir}/%{cpan_dist_name} \
...
...
...
-A 檢視passwd中包含home字元的行以及列出其下面2行
[[email protected] grep]# grep -nA2 'home' passwd
17:user:x:1000:1000:user:/home/user:/bin/bash
18:admin:x:1001:1001::/home/admin:/bin/bash
19-gluster:x:990:984:GlusterFS daemons:/var/run/gluster:/sbin/nologin
20-unbound:x:989:983:Unbound DNS resolver:/etc/unbound:/sbin/nologin
-B 檢視passwd中包含home字元的行以及列出其上面2行
[[email protected] grep]# grep -nB2 'home' passwd
15-sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
16-tcpdump:x:72:72::/:/sbin/nologin
17:user:x:1000:1000:user:/home/user:/bin/bash
18:admin:x:1001:1001::/home/admin:/bin/bash
-C 檢視passwd中包含home字元的行以及列出其上下2行
15-sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
16-tcpdump:x:72:72::/:/sbin/nologin
17:user:x:1000:1000:user:/home/user:/bin/bash
18:admin:x:1001:1001::/home/admin:/bin/bash
19-gluster:x:990:984:GlusterFS daemons:/var/run/gluster:/sbin/nologin
20-unbound:x:989:983:Unbound DNS resolver:/etc/unbound:/sbin/nologin
grep例項2
grep ‘[0-9]’ filename 檢視檔案中包含數字0-9的行 grep -v ‘[0-9]’ filename 檢視檔案中不包含數字的行 grep -v ‘^#’ filename 檢視檔案中不帶#號的行 grep ‘[^0-9]’ filename 檢視非0-9的字元,即包含0-9的字元不被高亮顯示 grep ‘[0-9]’ filename 篩選非數字開頭的行 grep ‘r.o’ filename 篩選包含“r與o”的行 “.”為任意字元 grep -n ‘o*o’ filename 前面表示0-n次出現,後面表示前面匹配的字元應該與後面的一致。及多次出現該字元都會被匹配進來。 grep '.’ passwd filename 匹配任意符號任意字元 grep ‘o{2}’ filename 表示篩選帶有兩個o字元的行 grep -n ‘o{0,3}’ filename 表示篩選0-3個帶有o字元的行 egrep–>grep -E 不使用反義符的情況下使用 egrep -n ‘o+o’ filename +與“”星號類似,但是+表示為1-n次出現,不包含0次 egrep -n ‘o?t’ filename “?”表示問號前面出現的字元為0或者1次,有則出現,沒有這隻匹配後面的字元出現 egrep -n ‘roo|nologin’ filename 同時篩選roo與nologin字元所在的行
檢視passwd中包含0-9的所有行
檢視/etc/inittab不包含數字的行 grep -nv ‘[0-9]’ /etc/inittab grep -n ‘[0-9]’ /etc/inittab 分別顯示了帶數字與不帶數字的行
複製/etc/inittab到當前目錄下 同時編輯inittab,手動新增一行不帶#號的行 grep -nv ‘^#’ inittab 顯示不帶#號的行 grep -n ‘^#’ inittab 顯示帶#號的行
grep ‘[^0-9]’ inittab 檢視非0-9的字元,即包含0-9的字元不被高亮顯示,純數字的行不顯示。
grep ‘[0-9]’ inittab 篩選inittab中非數字開頭的行
grep ‘r.o’ passwd 篩選包含“r與o”的行 “.”為任意字元 其中8,9行為自行手動新增
grep -n ‘o*o’ passwd 前面表示0-n次出現,後面表示前面匹配的字元應該與後面的一致。及多次出現該字元都會被匹配進來。 grep -n '.’ passwd "."表示任意符號任意字元 如需要篩選root開頭 bash結尾的行
grep -n ‘o{2}’ passwd 表示篩選帶有兩個o的行
grep -n ‘o{0,3}’ passwd 表示篩選0-3個帶有o的字元
如果在不使用反義符號的情況下可以使用egrep或者grep -E egrep–>grep -E
egrep -n ‘o+o’ passwd +與“*”星號類似,但是+表示為1-n次出現,不包含0次
egrep -n ‘o?t’ passwd egrep -n ‘o?1o’ passwd “?”表示問號前面出現的字元為0或者1次,有則出現,沒有這隻匹配後面的字元出現
egrep -n ‘roo|nologin’ passwd 同時篩選roo與nologin字元所在的行