关于grep和egrep命令的作业

目录

例题类型一:

例题类型二:


例题类型一:

1、显示/etc/rc.d/rc.sysinit文件中以不区分大小的h开头的行;

[root@node1 a1]# egrep -i '^h' /etc/rc.d/rc.sysinit
[root@node1 a1]# egrep 'h|H' /etc/rc.d/rc.sysinit
[root@node1 a1]# egrep '[hH]' /etc/rc.d/rc.sysinit

2、显示/etc/passwd中以sh结尾的行;

[root@node1 a1]# grep -E 'sh$' /etc/passwd
root:x:0:0:root:/root:/bin/bash
fox:x:1000:1000::/home/fox:/bin/bash

3、显示/etc/fstab中以#开头,且后面跟一个或多个空白字符,而后又跟了任意非空白字符的行

[root@node1 a1]# grep -E '^#\s+\S*' /etc/fstab 
# /etc/fstab
# Created by anaconda on Thu Sep  7 05:25:48 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@node1 a1]# egrep '^#[[:space:]]+[^[:space:]]*' /etc/fstab 
# /etc/fstab
# Created by anaconda on Thu Sep  7 05:25:48 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@node1 a1]# grep -E '^# +[^ ]*' /etc/fstab 
# /etc/fstab
# Created by anaconda on Thu Sep  7 05:25:48 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.

4、查找/etc/rc.d/rc.loca]中包含“以to开始并以to结尾”的字串行:

[root@node1 a1]# egrep '\bto.*to\b' /etc/rc.d/rc,local

5、查找/etc/inittab中含有“以s开头,并以d结尾的单词”模式的行;

[root@node1 a1]# grep -w 's.*d' /etc/inittab 
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:

6、查找ifconfig命令结果中的1-255之间的整数;

[root@node1 a1]# ifconfig| egrep -w '[1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]'
        inet 192.168.17.129  netmask 255.255.255.0  broadcast 192.168.17.255
        inet6 fe80::20c:29ff:fee5:4a1d  prefixlen 64  scopeid 0x20
        ether 00:0c:29:e5:4a:1d  txqueuelen 1000  (Ethernet)
        RX packets 6413  bytes 558548 (545.4 KiB)
        TX packets 4359  bytes 415704 (405.9 KiB)
lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        RX packets 17  bytes 2039 (1.9 KiB)
        TX packets 17  bytes 2039 (1.9 KiB)

7、显示/var/log/secure文件中包含“Failed”或“FAILED”的行

[root@node1 a1]# grep -i 'Failed'  /var/log/secure

8、在/etc/passwd中取出默认she11为bash

[root@node1 a1]# grep -w 'bash' /etc/passwd
root:x:0:0:root:/root:/bin/bash
fox:x:1000:1000::/home/fox:/bin/bash

9、以长格式列出/etc/目录下以ns开头、.conf结尾的文件信息

[root@node1 a1]# ls /etc | grep '\bns.*.conf\b'
nsswitch.conf
nsswitch.conf.bak

10高亮显示passwd文件中冒号,及其两侧的字符

[root@node1 a1]# egrep '.:.' /etc/passwd

11、匹配/etc/services中开头结尾字母一样的单词

理解一:

[root@node1 a1]# egrep  '^(\b[a-zA-Z]+\b).*\1$' /etc/services 
关于grep和egrep命令的作业_第1张图片

理解二:

[root@node1 a1]# egrep -w '\b([a-zA-Z])\w*\1\b' /etc/services 关于grep和egrep命令的作业_第2张图片

  • \b:表示单词边界,确保我们匹配的是整个单词。
  • ([a-zA-Z]):匹配一个字母,并使用括号捕获它,以便在后面的引用中使用。
  • \w*:匹配零个或多个字母、数字、下划线字符。
  • \1:引用第一个捕获组中的内容,即开头的字母。
  • \b:再次使用单词边界来确保我们匹配的是整个单词

例题类型二:

创建文件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

1.含有“48”字符串的行的总数

[root@node1 a1]# grep -c '48' file.txt 
4
[root@node1 a1]# grep '48' file.txt |wc -l
4

2.显示含有“48”字符串的所有行的行号

[root@node1 a1]# grep -n '48' file.txt 
1:48   Dec    3BC1977    LPSX    68.00   LVX2A   138
3:483 Sept   5AP1996     USP    65.00   LVX2C   189
9:484  nov      7PL1996   CAD    49.00     PLV2C  234
11:483   may     5PA1998  USP     37.00    KVM9D  644

3.精确匹配只含有“48字符串的行、

[root@node1 a1]# grep -w '48' file.txt 
48   Dec    3BC1977    LPSX    68.00   LVX2A   138
[root@node1 a1]# grep '\b48\b' file.txt 
48   Dec    3BC1977    LPSX    68.00   LVX2A   138

4.抽取代码为484和483的城市位置

[root@node1 a1]# egrep '(484|483)' file.txt                    #因为有()因此需要使用egrep
483 Sept   5AP1996     USP    65.00   LVX2C   189
484  nov      7PL1996   CAD    49.00     PLV2C  234
483   may     5PA1998  USP     37.00    KVM9D  644

5.显示行首不是4或8

[root@node1 a1]# egrep -v '^(4|8)' file.txt

219  dec      2CC1999  CAD     23.00   PLV2C    68

216   sept      3ZL1998  USP     86.00    KVM9E   234
[root@node1 a1]# egrep ^[^48] file.txt 
219  dec      2CC1999  CAD     23.00   PLV2C    68
216   sept      3ZL1998  USP     86.00    KVM9E   234

[root@node1 a1]# egrep -v '^4|^8' file.txt 

219  dec      2CC1999  CAD     23.00   PLV2C    68

216   sept      3ZL1998  USP     86.00    KVM9E   234

6.显示含有九月份(Sept)的行

[root@node1 a1]# grep  -i 'sept' file.txt 
483 Sept   5AP1996     USP    65.00   LVX2C   189
216   sept      3ZL1998  USP     86.00    KVM9E   234

7.显示以K开头,以D结尾的所有代码

[root@node1 a1]# grep 'K.*D' file.txt 
47    Oct      3ZL1998    LPSX   43.00   KVM9D 512
483   may     5PA1998  USP     37.00    KVM9D  644

8.显示头两个是大写字母,中间至少两个任意,并以C结尾的代码

[root@node1 a1]# egrep '\b[A-Z]{2,}.{2,}C\b' file.txt 
483 Sept   5AP1996     USP    65.00   LVX2C   189
219  dec      2CC1999  CAD     23.00   PLV2C    68
484  nov      7PL1996   CAD    49.00     PLV2C  234

9.查询所有以5开始以1996或1998结尾的所有记录  

[root@node1 a1]# egrep '\b5.*(1996|1998)\b' file.txt 
483 Sept   5AP1996     USP    65.00   LVX2C   189
483   may     5PA1998  USP     37.00    KVM9D  644

关于grep和egrep命令的作业_第3张图片

你可能感兴趣的:(Linux,linux)